- Home /
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 :P 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();
}
}
Answer by GiyomuGames · Dec 10, 2015 at 07:04 AM
You are probably trying to use the animators while your object is inactive.
Answer by BackslashOllie · Dec 10, 2015 at 05:20 PM
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>();
Oopps no i do not.. i put them into the start function.. but the same exact thing happened.. I also tried making them inactive when (PlayerScript.dead) was true.. but it says the Animator doesn't allow ".SetActive" methods to be used.. Any clue what to do?
I think Guidanel has got it in one. You just need to step through your code and see when your code is trying to call the animator whilst its gameobject is inactive.
Your answer
Follow this Question
Related Questions
Attempting to change AnimatorController during Runtime 0 Answers
Animator parameter does not pick the value of the animation curve with the same name 2 Answers
Wrap Mode in animator not working properly 0 Answers
Display additional animation on top of other animations 0 Answers
Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer