- Home /
Can't play animation after setting GameObject to inactive and back to active again
I have an UI element which has an Animator component attached with a simple looping animation that fades it in and out.
This UI element needs to be turned on or off on multiple occasions, so I have a script that disables it as needed using SetActive(false).
However, though it animates correctly at the start of the game, as soon as I've set this to SetActive(false) and then SetActive(true) again, or if any of its parent GameObjects go through the same process, the looping animation stops and I cannot figure out how to make it start playing again.
I've tried manually playing the state with .Play("statename"), as well as manually turning the actual Animator component on and off again with no luck. This is an extremely simple application, so I was hoping I'm missing something obvious here...
Edit: As mentioned in the comment chain of the original post, it turns out that the cause is the UI window setting Time.timeScale to 0 to pause the game while open, which naturally also pauses the animation. Can this be overridden or circumvented somehow? I'll need to use these kind of UI animations while the game is paused, and setting the timeScale to 0 seems like the most reliable way to do this.
is your animation connected with you default state in the animator?
Yes - this is what it looks like:
The animation itself has loop time set to true.
when you are in playmode what happens? the flash animation should be played. the blue line that shows which animation is being played is not even appearing?
Answer by ivarhill · Mar 11, 2019 at 04:23 PM
Setting this as resolved since the issue is separate from the original question. Thanks to @xxmariofer for suggestions!
he y man i'm dealing with the exact same problem as you. Did you manage to solve the problem and if so, how?
check the comments of the question for the full discusion but his issue was setting time.scale to 0
"That's a good idea! I did some testing and I think I finally found the root cause - while the code to open the UI window itself runs, it sets Time.timeScale to 0 to pause the game, which also pauses the animation. Obvious in hindsight...
Is there a good way to get around this? I need to pause the game while the UI is open, but I also need animations like these to play, so maybe there's a different method entirely that makes sense."
Answer by drHogan · Oct 24, 2019 at 10:27 AM
You probably solved it by now, but in the inspector you can change to Unscaled Time the animator update, this way it will not be influenced by the game timescale.
Answer by cs120319992 · Mar 11, 2019 at 03:51 PM
Try to enable and disable self animator on object.
As in set the animator component to disabled and enabled again once the GameObject has been activated? I've tried this-
Animator flashAnimator = flashEffect.GetComponent<Animator>();
flashAnimator.enabled = false;
flashAnimator.enabled = true;
flashAnimator.Play("flash");
But it doesn't seem to make a difference. For reference, this is what the Animator looks like while running:
It just sits still with the blue bar at that position and does not move at all.
Answer by Fingall · May 31, 2021 at 08:05 AM
you shoude use this: if (isActiveAndEnabled) { animator.CrossFade(acname, 0f, -1); }
Your answer
Follow this Question
Related Questions
How to loop an animation in reverse? 0 Answers
How to make a 2d animation repeat 1 Answer
Stop animation in Animation Controller 0 Answers
Animation Loop 2 Answers
How can I reset a non-looping animation? 0 Answers