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.
– MilitaryGOk 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.
– MilitaryG4640 is my discord id don't call me we can chat if you want
– MilitaryGOk try this: public class Running : MonoBehaviour{ void Start(){ DontDestroyOnLoad(this.gameObject); DontDestroyOnLoad(animator); } }
– MilitaryG