MissingReferenceException: The object of type 'Animator' 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 have an endless runner game and my player can respawn after wathing an ad. But after the player goes to the market page, or just tries again without reviving, unity gives the error
"MissingReferenceException: The object of type 'Animator' 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'm using LoadScene function to restart the game or coming back from the market.
This is my code for restarting the game after death.
public void TryAgain()
{
PlayerPrefs.SetInt("TotalMoney", numberOfCoins + PlayerPrefs.GetInt("TotalMoney"));
SceneManager.LoadScene(0);
scoreMultiplier = 1;
numberOfBullets = 0;
}
And this is the one that works after the ad ends. But error says it cant find the animator in here.
public void Revived()
{
moveSpeed = 10;
PlayerManager.scoreMultiplier = 1;
death = false;
animator.SetTrigger("Revive");
deathStuff.SetActive(false);
}
Just set the gameObject to dont destroy on load like this:
DontDestroyOnLoad(this.gameObject);//the animator gameObject
SceneManager.LoadScene(0);
This is how unity understands:
Scene 0 go1 on RAM 0001
Scene 1 go1 on RAM 0002
Than in scene 1 you are trying to access 0001 witch is destroyed.
The problem is, the stuff is not actually destroyed. They are there and even referenced in the script. But when the script is trying to use them, it gives me this error.
Just looking more clearly at your error
This script has nothin to do with animator except scene
The animator is destroyd
This is very clever. So can you tell me how to fix it? Because i'm lost.
The animator GameObject find it and attach it
Don't know where is it can't help if I'm blind
Try
In TryAgain()
Grab script in witch is giving you error
And set it don't destroy on load as written above
It didnt worked and i just noticed something. There are some other functions in this script that uses the same animator. And they dont give any errors. Only this specific function is having an error with the animator. Is that normal?
Ok I won't be able to get anything done if I won't see full codes
Edit your post and post full codes with using included.
If that isn't done I can't help.
Ok try this:
public class Running : MonoBehaviour{
void Start(){
DontDestroyOnLoad(this.gameObject);
DontDestroyOnLoad(animator);
}
}
And if you add:
DontDestroyOnLoad(this);
Might be enough
Getting the same error. Somehow, Revive() function cannot find the animator when other ones can.
4640 is my discord id don't call me we can chat if you want
I thought i can share my screen with you so we can work this out faster.
Answer by ashkatchen · Nov 28, 2021 at 05:27 PM
I wrote
if(animator == null)
{
animator = GetComponentInChildren<Animator>();
}
in the update function and it worked.
Answer by jeff13gaydos · Apr 08 at 10:20 AM
For anyone looking for a less "patchy" solution, my errors had to do with not unsubscribing some callbacks in scripts that were in an observer pattern (you should do this OnDestroy), and then also some scriptable objects were referencing destroyed objects (since scriptable objects are persistent). The above solution will work in all (if not most) situations regardless.