- Home /
 
MissingRefrenceException help for enemyai
I have some code for a simple ai. My game has a wave system and respawning but when the player dies the game stops and I get this error MissingRefrenceException The object type "Transform" has been destroyed but you are still trying to access it. I know I have to put in a statement to check if the transform is null but I just don't know where or what to do if it is null. Any help is appreciated, here is the code. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed;
 private Transform myTransform;
 // Use this for initialization
 void Awake()
 {
     myTransform = transform;
 }
 void Start()
 {
     GameObject go = GameObject.FindGameObjectWithTag("Player");
     target = go.transform;
 }
 // Update is called once per frame
 void Update()
 {
    
     Vector3 dir = target.position - myTransform.position;
     dir.z = 0.0f; // Only needed if objects don't share 'z' value
    
     if (dir != Vector3.zero)
     {
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
                                                  Quaternion.FromToRotation(Vector3.right, dir), rotationSpeed * Time.deltaTime);
     }
     
     //Move Towards Target
     myTransform.position += (target.position - myTransform.position).normalized * moveSpeed * Time.deltaTime;
       
     
 }
 
               }
Answer by BaldBeardedMonk · Apr 28, 2017 at 06:20 AM
 void update()
 {
      if (go!=null)
     {
       //rest of the code here 
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
I have an error, script should check for null or not destroy game objects 2 Answers
How can throw "Missing reference exception" this gameObject?.Getcomponent<>() ?? 2 Answers
Powerup only spawning twice (MissingReferenceException Error) 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers