Hello guys. I was hoping you guys can help me! I am making a game where minions fight, and I want that after a minion kills another, he changes his target to other minion.
However I get this error after destroying the gameObject.
MissingReferenceException: The object of type ‘Transform’ 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.
I leave you my code so if you guys can please point me in the right direction or tell me what I am doing wrong, I would be very grateful!!!
public class aController : MonoBehaviour
{
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public float atackDistance;
private Transform myTransform;
private GameObject go;
void Awake(){
myTransform = transform;
}
// Use this for initialization
void Start () {
SetTarget();
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.transform.position, myTransform.position, Color.yellow);
//finds target
if (target == null)
{
SetTarget ();
}
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if ((Vector3.Distance(target.position, transform.position)) > atackDistance)//Move until in range to atack
{
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else{
atack ();
}
}
void SetTarget()
{
go = GameObject.FindGameObjectWithTag("teamB");
target = go.transform;
}
void atack()
{
//ATACK CODE HERE...
}
}
Thank you!