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

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

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

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

Q&A

解決済

1回答

958閲覧

[Unity] Update();関数が使えない理由が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

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

0グッド

0クリップ

投稿2021/08/06 02:54

提示コードですが"Update()関数が利用されていません"というメッセージが出るのですが何が原因なのでしょうか? スクリプトと同じ名前のクラス名を用意してそこにvoid Update()を乗せているのですが原因がわかりません。

参考サイト: https://squmarigames.com/2018/11/20/unity-beginner-class/

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

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

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

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

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

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

fiveHundred

2021/08/06 05:57 編集

これが原因かどうかは分かりませんが、「move = new MoveControl();」や「attack = new AttackControl();」ということはやってはいけません。 MonoBehaviourを継承したクラスにnewをするのは非推奨ですし、そうでなくともシーン上などに配置されたものではなくnewで新規作成されたものにアクセスすることになるので、正常に動作しません。
guest

回答1

0

ベストアンサー

「move = new MoveControl();」や「attack = new AttackControl();」
を行っているためでした。

投稿2021/08/06 06:00

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

YT0014

2021/08/07 03:35

moveやattackの正しい生成方法がないので、エラーメッセージは出なくなるかもしれませんが、期待通りの動作になりません。 エラー箇所の削除をした、というのと、本質的に同義です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問