- Home /
Animator has not been initialized??
I've been working on a Client type of UI. The buttons all have transition type Animation, with an attached animator. When buttons are disabled, they scale down. When enabled, they return to (or just are) normal scale. For some reason, when I run my function to reset everything, I sometimes get the error "Animator not Initialized" and sometimes dont, but the transition animations just stop playing. I'm 100% sure I'm not setting the animator null anywhere in the code, and I've tried button.interactable = true, button.animator.SetTrigger("Normal"), and button.GetComponent().SetTrigger("Normal"). The buttons stay scaled down (but ARE interactable again)
What's happening?
Can you link some code please? It's much easier to find problems if you like the problem code :)
$$anonymous$$y code is split up between 5 big scripts (and a bunch of tiny ones) :/ I was hoping this would be a common error that people might know the cause of
Answer by Julien-Lynge · Feb 03, 2015 at 05:29 PM
When the GameObject containing an animator is turned on, the animator starts uninitialized, and is initialized that frame. If you attempt to call methods on the animator in the same frame it's being initialized, the call may go through before the initialization finishes, and Unity will refuse to run the command. The transition won't occur because you never actually set the trigger. The solution would be to wait a frame after turning on the object, and then call your method.
I believe that the initialization happens each time the GameObject is activated, not just the first time. As an alternative to waiting a frame, you could try LateUpdate or yield return new WaitForEndOfFrame().
WaitForEndOfFrame() sometime isn't enough for the animator to be ready (initialized), your best bet is to yield return new WaitUntil(() => animator.isInitialized);
Answer by RJ45 · Aug 04, 2016 at 05:05 AM
Put it in a coroutine and wait until the animation isInitialized flag is true.
i.e : yield return new WaitUntil( () => animator.isInitialized );
Thanks for the tip :)
Answer by ndcv · Mar 17, 2015 at 09:15 PM
I was having this same problem and this thread helped me track it down...
I have a CanvasGroup holding my three buttons. The CanvasGroup had interactable set to "false" and alpha to zero, so the menu wouldn't be there on load. The Buttons are using Animation transitions, and the CanvasGroup has an animator to fade the whole menu in and out.
My workaround/solution so far is to leave the CanvasGroup interactable = true, but the default animation state is one that sets it to false. The warnings are completely gone now.
Down side is, I think that there now might be some amount of time (hopefully only one frame) at load when the invisible buttons are interactable. Not sure if that will cause any problems or not.