GameObject destroyed

I keep getting a MissingReference Exception: 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 destroy the object. And it sends me to the Portal.cs code where the player is trying to use NavMeshAgent.Warp(otherPortal.spawnPoint.position); and after it fades out and then in when the new scene loads I get that error. I found if I moved the UpdatePlayer(otherPortal) to below the Destroy(gameObject); line in the code the error goes away.
I still haven’t figured out why it shows this error. Even when I get the course code for this section off of github it does the same error.

Okay I figured out what I needed to do to fix the error above. in the UpdatePlayer method I had to do a null check on otherPortal like this:

void UpdatePlayer(Portal otherPortal)
{
     if(otherPortal != null)
     {
          player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);
          player.GetComponent<NavMeshAgent>().enabled = true;
          player.transform.rotation = otherPortal.spawnPoint.rotation;
    }
}

And I have removed the UpdatePlayer(otherPortal) from after the Destroy(gameObject) in the Transition IEnumerator and the error went completely away.

Destroy(gameObject) should be the last thing in your Transition coroutine.

It is I removed the UpdatePlayer after Destroy(gameObject) because it didn’t fix the error that I was getting. The second post where I replied saying “Okay I figured it out”, shows I had to put a null check for the portal and put all three player components of UpdatePlayer into the

If(otherPortal != null)
{
player.GetComponent().Warp(otherPortal.spawnPoint.position);
player.GetComponent().enabled = true;
player.transform.rotation = otherPortal.spawnPoint.rotation;
}

This actually fixed the error I was receiving “MissingReference Exception: The object of Type ‘GameObject’ has been destroyed but you are still trying to access it”

Thanks, Brian. I just wanted to post a solution in a reply instead of a new post, because I thought others could use this. If they have received the same error as I did.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms