unityでゲームを作ろうと思っています。
ボールを動かし、そのボールから最も近い距離にあるキューブの色を変更したいです。
色を変更するスクリプト(colorc)はキューブにアタッチしており、ボールを動かして最も距離が近くなった際にそのスクリプトをアクティブにしようと思っています。(シーン開始時そのスクリプトは非アクティブ)
以下を実行したのですが、Object reference not set to an instance of an objectとエラーが出てしまいます。
何が原因でしょうか?
C#
1using UnityEngine; 2using System.Collections; 3 4 5 public class Distance_detection : MonoBehaviour 6 { 7 private GameObject nearObj; 8 private float searchTime = 0; 9 10 // Use this for initialization 11 void Start() 12 { 13 14 nearObj = serchTag(gameObject, "Player"); 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 searchTime += Time.deltaTime; 21 if (searchTime >= 1.0f) 22 { 23 nearObj = serchTag(gameObject, "Player"); 24 searchTime = 0; 25 } 26 //nearObj = GameObject.Find("Sphere").GetComponent<Renderer>(); 27 //nearObj.material.color = Color.yellow; 28 //nearObj.transform.LookAt(nearObj.transform); 29 //nearObj.transform.Translate(Vector3.forward * 0.01f); 30 nearObj.GetComponent<colorc>().enabled = true; 31 32 } 33 34 GameObject serchTag(GameObject nowObj, string Player) 35 { 36 float tmpDis = 0; 37 float nearDis = 0; 38 GameObject targetObj = null; 39 40 41 foreach (GameObject obs in GameObject.FindGameObjectsWithTag(Player)) 42 { 43 tmpDis = Vector3.Distance(obs.transform.position, nowObj.transform.position); 44 if (nearDis == 0 || nearDis > tmpDis) 45 { 46 nearDis = tmpDis; 47 targetObj = obs; 48 } 49 50 } 51 52 return targetObj; 53 } 54 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/27 07:13
2017/11/27 09:02
2017/11/28 15:42