前提・実現したいこと
PlayerがEnemyの当たり判定に当たるとPlayerをデストロイさせたいんですが
MissingReferenceExceptionのエラーが出て途中で止まってしまいます。
ネットで調べたところNULLをする必要があるらしくどこに追加すればいいか分からなくて
変えてもエラーが出てしまいます。
該当のソースコード
search.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class search : MonoBehaviour { public bool playerClosing; private SphereCollider sphereCol; // Use this for initialization void Start() { sphereCol = GetComponent<SphereCollider>(); } // Update is called once per frame void OnTriggerStay(Collider col) { if (col.gameObject.tag == "Player") { playerClosing = true; sphereCol.radius = 10f; } } void OnTriggerExit(Collider col) { playerClosing = false; sphereCol.radius = 5f; } }
Enemy.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { private float speed = 350f; private float rotationSmooth = 1f; private Vector3 targetPosition; private float changeTargetSqrDistance = 40f; private Rigidbody rb; public bool playerclosing; public GameObject searching; public GameObject player; public float attackPoint = 10f; // Use this for initialization void Start() { //targetPosition = GetRandomPositionOnLevel(); rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update() { playerclosing = searching.GetComponent<search>().playerClosing; //現在地から目標地点までの距離を取得、短かったならもう一度目標地点を設定しなおす。 float sqrDistanceToTarget = Vector3.SqrMagnitude(transform.position - targetPosition); if (sqrDistanceToTarget < changeTargetSqrDistance) { targetPosition = GetRandomPositionOnLevel(); } //もし索敵範囲内にプレイヤーがいたら目標地点をプレイヤーのいる場所に変更する if (playerclosing) { targetPosition = player.transform.position; } //目標地点の方向を向く Quaternion TargetRotation = Quaternion.LookRotation(targetPosition - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, TargetRotation, Time.deltaTime * rotationSmooth); //前へ進む rb.velocity = (transform.forward * speed * Time.deltaTime); //Debug.DrawLine(transform.position,targetPosition,Color.red); } public Vector3 GetRandomPositionOnLevel() { float levelSize = 10f; return new Vector3(Random.Range(-levelSize, levelSize), 0, Random.Range(-levelSize, levelSize)); //ここで処理を終了(return)し、この関数を開始する直前の位置へ戻る } }
Attack.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Attack : MonoBehaviour { public bool playerAttak; private BoxCollider boxCol; public GameObject player; // Use this for initialization void Start() { boxCol = GetComponent<BoxCollider>(); player = GameObject.FindWithTag("Enemy"); } // Update is called once per frame void OnTriggerStay(Collider col) { if (col.gameObject.tag == "Player") { playerAttack = true; Destroy(gameObject); } } }
エラー内容 MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.GameObject.GetComponent[search] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:38) Enemy.Update () (at Assets/Enemy.cs:33)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/18 13:20
2018/07/18 13:42