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;
}
}