Object is destroyed, but script is trying to access it

Hello guys, i have little boring issue…
I get this error:
MissingRefferenceException: The object of type ‘Transform’ has been destreyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

When i click the error, it highlights this line of code:

agent.SetDestination(target.position);

This happens when i click my retry button ( the button restart the same scene from the beginning )

The whole script:

using UnityEngine;
using UnityEngine.AI;

public class AIMove : MonoBehaviour {

NavMeshAgent agent;
public Transform target;
void Start ()
{
agent = GetComponent ();
}

void Update ()
{
agent.SetDestination(target.position);
}

}

Pretty sure Unity’s Console isn’t making spelling mistakes :slight_smile: Next time, you can simply click the message and use CTRL+C to copy it. It’s so much easier.

Also, please use code tags to make the code readable: https://forum.unity3d.com/threads/using-code-tags-properly.143875/

It means that it can no longer find the Transform named target. You should make sure you have target assigned in the Inspector, and that you aren’t destroying it while playing.

I know what it means, I just dont know how to fix it. I’ve tried to asd this to the script loop, but it doesn’t help.

  • if (AIMove != null) {
  • }

Or I have to write NavMeshAgent instead of the class?

I think you should do if(target!=null), since if I understood correctly the target is null

I found the problem, I had destroy player function in another script. Removed it and I dont get the error anymore, but script dont work correctly anymore too. One win, one lose. :slight_smile: