前提・実現したいこと
ここに質問の内容を詳しく書いてください。
Unityでarrowとplayerの衝突を手書きで設定しようとしているのですが、エラーが出てきます。
player側から、arrowPrefabをFindで見つけてくるところで問題があるのだろうと思い、Debub.Logを設定してみたら、やはりnullでした。しかし、なぜnullになるのかがわかりません。
起動時にplayerはいても、一瞬だけarrowの生成に時間がかかるのではと思ったりもして、下の方ではnullになった時の対策を入れてみたりしていますが。。
一応、arrowはしっかり飛んではいるので、このプログラムの問題だと思います。なにかヒントでも良いので頂けると嬉しいです^^
よろしくお願いします。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object playerController.Update () (at Assets/playerController.cs:29)
該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class playerController : MonoBehaviour { Vector2 arrowPos; Vector2 playerPos; Vector2 dir; GameObject arrow; // Use this for initialization void Start() { } // Update is called once per frame void Update() { this.arrow = GameObject.Find("arrowPrefab(Clone)"); Debug.Log(arrow); if (Input.GetKeyDown(KeyCode.LeftArrow)) { transform.Translate(-1, 0, 0); } else if (Input.GetKeyDown(KeyCode.RightArrow)) { transform.Translate(1, 0, 0); } playerPos =transform.position; arrowPos = arrow.transform.position; if (arrowPos == null) { arrowPos = new Vector2(0, 0); } else { dir = (playerPos - arrowPos); Debug.Log(arrowPos); } // if (dir.magnitude < 1.5) { } } }
試したこと
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
Unity 2018
VisualStudio2017
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/27 07:18 編集