質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1435閲覧

[Unity] 他のクラスをインスタンスする方法が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/08/06 03:33

提示子コードですが他のクラスをインスタンスしようとすると以下のような警告が出るのですがこれはどうやって回避するのでしょうか?調べましたが出てきません。newを使わないインスタンス方法があるのでしょうか?

参考サイト: https://ufcpp.net/study/csharp/

警告メッセージ[ You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor () ]

日本語訳 [ 'new'キーワードを使用してMonoBehaviourを作成しようとしています。 これは許可されていません。 MonoBehavioursは、AddComponent()を使用してのみ追加できます。 または、スクリプトはScriptableObjectから継承することも、基本クラスをまったく継承しないこともできます。
UnityEngine.MonoBehaviour:.ctor() ]

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.Linq; 5 6public class Player_Control : MonoBehaviour 7{ 8 private MoveControl move; 9 private AttackControl attack; 10 11 12 void Awake() 13 { 14 move = new MoveControl(); 15 attack = new AttackControl(); 16 move.Init(); 17 } 18 19 void Start() 20 { 21 22 } 23 24 void Update() 25 { 26 Debug.Log("あああ"); 27 move.Move(); 28 attack.Attack(); 29 attack.SelectWeapon(); 30 attack.SelectWeapon_View(); 31 32 } 33 34 private void FixedUpdate() 35 { 36 attack.Attack_FixedUpdate(); 37 } 38 39 40 void LateUpdate() 41 { 42 43 } 44 45 46 47 48 49 50 51} 52 53 54// 移動管理クラス 55public class MoveControl : MonoBehaviour 56{ 57 private CharacterController cc; 58 private Animator anim; 59 60 61 private Vector3 moveForward; 62 [SerializeField, Range(0.001f, 10)] private float moveSpeed; 63 64 private float inputHorizontal; 65 private float inputVertical; 66 67 public MoveControl() 68 { 69 70 } 71 72 public void Init() 73 { 74 anim = GetComponent<Animator>(); 75 cc = GetComponent<CharacterController>(); 76 } 77 78 public void Move() 79 { 80 81 82 if (Input.GetKey(KeyCode.W)) 83 { 84 inputVertical = 1; 85 } 86 else if (Input.GetKey(KeyCode.S)) 87 { 88 inputVertical = -1; 89 } 90 else 91 { 92 inputVertical = 0; 93 } 94 95 if (Input.GetKey(KeyCode.A)) 96 { 97 inputHorizontal = -1; 98 } 99 else if (Input.GetKey(KeyCode.D)) 100 { 101 inputHorizontal = 1; 102 } 103 else 104 { 105 inputHorizontal = 0; 106 } 107 108 Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward.normalized, new Vector3(1, 0, 1)).normalized; 109 moveForward = (cameraForward * inputVertical) + (Camera.main.transform.right * inputHorizontal); 110 111 if (moveForward != Vector3.zero) 112 { 113 transform.rotation = Quaternion.LookRotation(moveForward); 114 } 115 116 if (Input.GetKeyDown(KeyCode.Space)) 117 { 118 if (transform.GetComponentInChildren<Player_Ground_Check>().isGround == true) 119 { 120 cc.Move(new Vector3(0, 200, 0) * Time.deltaTime); 121 } 122 } 123 124 if (transform.Find("Ground_Check").GetComponent<Player_Ground_Check>().isGround == false) 125 { 126 anim.SetBool("isJump", true); 127 } 128 else 129 { 130 anim.SetBool("isJump", false); 131 } 132 133 anim.SetFloat("moveSpeed", moveForward.magnitude); 134 135 cc.Move(moveForward.normalized * moveSpeed * Time.deltaTime); 136 cc.Move(new Vector3(0, -9.8f, 0) * Time.deltaTime); 137 } 138} 139 140//攻撃管理 141public class AttackControl : MonoBehaviour 142{ 143 144 [SerializeField, Range(3, 10)] private float instancePositionDistance; // 145 [SerializeField, Range(3, 100)] private int objectPoolInstance; // 146 [SerializeField, Range(0.0f, 3.0f)] private float intervalTime; // 147 [SerializeField] private List<GameObject> weaponList; // 148 149 150 private List<List<GameObject>> instanceWeapon; // 151 private float time; // 152 private int listNumber; // 153 private GameObject weaponInstancePosition; // 154 private Vector3 forward; // 155 private bool isInstance; // 156 private GameObject nowInstance; //現在選択している武器 157 private bool isChangeWeapon; //武器切替 158 private List<GameObject> weapon; //武器切替オブジェクト 159 private Vector3 range; 160 161 162 // 現在の角度 163 private List<float> angle; 164 // 回転するスピード 165 [SerializeField] 166 private float rotateSpeed = 0.1f; 167 // ターゲットからの距離 168 [SerializeField] 169 private Vector3 distanceFromTarget; 170 171 172 173 174 public AttackControl() 175 { 176 time = 0; 177 isInstance = true; 178 weapon = new List<GameObject>(); 179 angle = new List<float>(); 180 181 182 weaponInstancePosition = transform.Find("WeaponInstancePosition").gameObject; 183 instanceWeapon = new List<List<GameObject>>(); 184 185 foreach (GameObject g in weaponList) 186 { 187 instanceWeapon.Add(new List<GameObject>()); 188 189 for (int i = 0; i < objectPoolInstance; i++) 190 { 191 instanceWeapon.Last().Add(Instantiate(g, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation)); 192 instanceWeapon.Last().Last().SetActive(false); 193 194 } 195 } 196 197 float r = 0; 198 for (int i = 0; i < weaponList.Count(); i++) 199 { 200 weapon.Add(CreateObject(i, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation)); 201 angle.Add(r); 202 r += 20; 203 } 204 205 206 207 208 listNumber = 0; 209 210 } 211 212 213 public void Attack() 214 { 215 Vector3 f = forward; 216 f.y = 0.20f; 217 218 weaponInstancePosition.transform.position = transform.position + f * instancePositionDistance; 219 220 if (nowInstance != null) 221 { 222 nowInstance.transform.position = weaponInstancePosition.transform.position; 223 nowInstance.transform.rotation = Quaternion.LookRotation(f) * Quaternion.Euler(90, 0, 0); 224 } 225 226 if (Input.GetMouseButtonDown(0)) 227 { 228 if ((time >= intervalTime) && (isInstance == false)) 229 { 230 nowInstance.transform.rotation = Quaternion.LookRotation(f) * Quaternion.Euler(90, 0, 0); 231 nowInstance.GetComponent<Bullet>().Active(f); 232 isInstance = true; 233 time = 0; 234 } 235 } 236 237 238 } 239 240 //武器選択アニメーション 241 public void SelectWeapon_View() 242 { 243 if (isChangeWeapon == true) 244 { 245 246 for (int i = 0; i < weaponList.Count(); i++) 247 { 248 weapon[i].gameObject.SetActive(true); 249 weapon[i].gameObject.GetComponent<Bullet>().ChangeViewMaterial(); 250 weapon[i].gameObject.transform.position = transform.position + Quaternion.Euler(0f, angle[i], 0f) * new Vector3(3, 3, 3); 251 angle[i] += rotateSpeed * Time.deltaTime; 252 253 if (angle[i] > 360) 254 { 255 angle[i] = 0; 256 } 257 } 258 } 259 260 261 262 } 263 264 265 public void SelectWeapon() 266 { 267 if (Input.GetKeyDown(KeyCode.Q)) 268 { 269 if (listNumber > 0) 270 { 271 nowInstance.gameObject.SetActive(false); 272 listNumber--; 273 isChangeWeapon = true; 274 nowInstance = CreateObject(listNumber, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation); 275 nowInstance.gameObject.GetComponent<Bullet>().SetActiveInstanceEffect(intervalTime); 276 } 277 } 278 else if (Input.GetKeyDown(KeyCode.E)) 279 { 280 if (listNumber < weaponList.Count - 1) 281 { 282 nowInstance.gameObject.SetActive(false); 283 listNumber++; 284 nowInstance = CreateObject(listNumber, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation); 285 nowInstance.gameObject.GetComponent<Bullet>().SetActiveInstanceEffect(intervalTime); 286 } 287 } 288 289 if (isInstance == true) 290 { 291 nowInstance = CreateObject(listNumber, weaponInstancePosition.transform.position, weaponInstancePosition.transform.rotation); 292 nowInstance.gameObject.GetComponent<Bullet>().SetActiveInstanceEffect(intervalTime); 293 isInstance = false; 294 } 295 } 296 297 public void Attack_FixedUpdate() 298 { 299 Vector3 f = Vector3.Scale(Camera.main.transform.forward.normalized, new Vector3(1, 0, 1)).normalized; 300 forward = (f + Camera.main.transform.forward).normalized; 301 302 time += Time.deltaTime; 303 } 304 305 306 //オブジェクトプール オブジェクト生成 307 GameObject CreateObject(int select, Vector3 pos, Quaternion rot) 308 { 309 foreach (GameObject gg in instanceWeapon[select]) 310 { 311 if (gg.activeSelf == false) 312 { 313 gg.SetActive(true); 314 gg.gameObject.GetComponent<Bullet>().Init(); 315 316 317 gg.transform.position = pos; 318 gg.transform.rotation = rot; 319 320 return gg; 321 } 322 } 323 324 instanceWeapon[select].Add(Instantiate(weaponList[select])); 325 instanceWeapon[select].Last().SetActive(true); 326 instanceWeapon[select].Last().transform.position = pos; 327 instanceWeapon[select].Last().transform.rotation = rot; 328 instanceWeapon.Last().Last().gameObject.GetComponent<Bullet>().Init(); 329 330 return instanceWeapon.Last().Last().gameObject; 331 } 332} 333

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

エラーメッセージで示されているように、AddComponentメソッドを使ってください。
GameObject-AddComponent - Unity スクリプトリファレンス

投稿2021/08/06 03:58

ku__ra__ge

総合スコア4524

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問