Animator has not been initialized!!!!!! Help!!

So i’ve been going through forms trying to figuire this problem out… but haven’t been able to fix it yet :stuck_out_tongue: So here’s my problem… I have 3 objects that use animators and are found on start of the scene… the first runthrough is fine… and works perfectly… But I have a restart function in my game… and when i restart the level… the animation is at its previously final state… and i get the error: “Animator has not been initialized.”

here’s the code:

using UnityEngine;
using System.Collections;

public class playScript : MonoBehaviour {

public bool playClicked = false;
public GameObject score;
public GameObject specials;
public GameObject pauseButton;
public GameObject deathMessage;
public GameObject endScore;
//public GameObject playButton;

public PlayerSpawn spawnPlayer;

Animator specialBarAnimator;
Animator scoreAnimator;
Animator pauseAnimator;

void Awake ()
{

}

void Start ()
{

	playClicked = false;
	score.SetActive (false);
	specials.SetActive (false);
	pauseButton.SetActive (false);
	StartCoroutine(Play());
	deathMessage.SetActive (false);
	endScore.SetActive (false);

}

void Update ()
{

	specialBarAnimator = specials.GetComponent<Animator>();
	scoreAnimator = score.GetComponent<Animator>();
	pauseAnimator = pauseButton.GetComponent<Animator>();

	if (PlayerScript.dead)
	{
		specialBarAnimator.SetTrigger ("Dead");
		scoreAnimator.SetTrigger ("Dead");
		pauseAnimator.SetTrigger ("Dead");
		deathMessage.SetActive (true);
		endScore.SetActive (true);
	}
}

IEnumerator Play () 
{
	yield return new WaitForSeconds(2);

	playClicked = true;

	score.SetActive (true);
	specials.SetActive (true);
	pauseButton.SetActive (true);

	spawnPlayer.CharacterSpawn();
}

}

You are probably trying to use the animators while your object is inactive.

Do you really need to be setting your animator variables every frame?

I would suggest putting these three lines into your Awake or Start functions before setting the GameObjects to inactive:

     specialBarAnimator = specials.GetComponent<Animator>();
     scoreAnimator = score.GetComponent<Animator>();
     pauseAnimator = pauseButton.GetComponent<Animator>();