前提・実現したいこと
UnityのNav Mesh Agent をもったオブジェクト(Cube_A)の挙動について質問です。
現在Cube_Aをプレハブにして、シーンには配置せず
時間経過でスクリプトから呼び出しています。
そのCube_AをDestroyもしくは非表示にした時に下記エラーが発生します。
内容的にシーンからはCube_Aをシーンからは消しているので
ごもっともなのですが、エラーが気持ち悪いので何か良い方法はありますでしょうか。
ご教示ください。
発生している問題・エラーメッセージ
"GetRemainingDistance" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.AI.NavMeshAgent:get_remainingDistance() test:Update() (at Assets/scripts/test.cs:76)
該当のソースコード
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.AI; 5 6public class test : MonoBehaviour 7{ 8 9 private NavMeshAgent agent; 10 private GameObject neardata; 11 private GameObject neargate; 12 13 14 // Use this for initialization 15 void Start () 16 { 17 agent = GetComponent<NavMeshAgent>(); 18 neardata = FindClosestData(); 19 neargate = FindClosestGate(); 20 agent.destination = neardata.transform.position; 21 } 22 23 void Update () 24 { 25 26 if (Input.GetKey("up") && this.gameObject.tag == "enemy") 27 { 28 //Destroy(this.gameObject); 29 gameObject.SetActive(false); 30 } 31 32 if (agent.remainingDistance<40.0f) 33 { 34 Debug.Log("a"); 35 gameObject.SetActive(false); 36 //Destroy(this.gameObject); 37 //agent.ResetPath(); 38 //neargate = FindClosestGate(); 39 //agent.SetDestination(neargate.transform.position); 40 41 } 42 43 } 44 45 46 public GameObject FindClosestData() 47 { 48 GameObject[] datas; 49 datas = GameObject.FindGameObjectsWithTag("data"); 50 GameObject closestdata = null; 51 float distance = Mathf.Infinity; 52 Vector3 position = transform.position; 53 foreach (GameObject data in datas) 54 { 55 Vector3 diff = data.transform.position - position; 56 float curDistance = diff.sqrMagnitude; 57 if (curDistance < distance) 58 { 59 closestdata = data; 60 distance = curDistance; 61 } 62 } 63 return closestdata; 64 } 65 66 public GameObject FindClosestGate() 67 { 68 GameObject[] gates; 69 gates = GameObject.FindGameObjectsWithTag("Gate"); 70 GameObject closestgate = null; 71 float distance = Mathf.Infinity; 72 Vector3 position = transform.position; 73 foreach (GameObject gate in gates) 74 { 75 Vector3 diff = gate.transform.position - position; 76 float curDistance = diff.sqrMagnitude; 77 if (curDistance < distance) 78 { 79 closestgate = gate; 80 distance = curDistance; 81 } 82 } 83 return closestgate; 84 } 85 86} 87
試したこと
・オブジェクトのDestroy,SetActive
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/15 07:42