- Home /
Effects of gameobject.setActive(false) on the animator.
tl;dr What happens to an animator when a script calls to GameObject.setActive(false) to the enclosing game object?
"Long" version:
We got this GameObject, with an Animator and a simple state machine and the controlling script. When something on the game happens, some functions and animations are called, with a final call to setActive(false) for the GameObject.
Then the GameObject is sent to a "respawn queue" where it is re-activated again after some time.
My "problem" is that, after reactivation, I need to move from "Idle despawn" to "Spawning", but somehow, the State Machine goes back again to "Idle spawned" (The default state), so my guess is that the state machine is resetting after the setActive(false) instruction. Still, I haven't been able to find any kind of information on this, just my spidersense tingling.
Any info on this topic? Maybe some suggestions on how to proceed with my problem?
Answer by Ed-Marty · Jan 16, 2017 at 08:36 PM
You are exactly correct. The state machine on any animator resets to its initial state when turned off/on.
If you need to run an animation from Idle despawn to Spawning, you could, upon enabling the animator, switch to the idle despawn state and then trigger the animation. Or, you could trigger this animation just before you disable it.
Either of those solutions could get messy though, unless you're careful.
Ok, thanks for this.
I'm using the default OnEnable function to do both transitions. I still need to check a bit our object lifecycle, as using that function seems to be a bit of a hack "to make this work".
$$anonymous$$nowing this I hope our next project starts with better design for those gameObjects, lol.
A possible workaround might be to not disable the gameobject, but to just make it "invisible" by disabling its renderers and colliders. That would would at least circumvent the reset of the animator. Not necessarily a nice solution but maybe it works for you.
Yeah, I thought on that but that's actually not possible. The GameObject is... a bit more complex of what I've said, and we decided time ago going the setActive(false) route to avoid clogging the code.
But yeah, it was our first idea (well, second, the first was just destroying it..) to handle the "despawn / respawn" cicle.
Answer by echoAndecho · Aug 13, 2020 at 09:43 AM
It's an old post but I'd like to point out there's a property now in the animator class: animator.keepAnimatorControllerStateOnDisable
which will prevent animator from resetting state when disabled. I'm using 2018.4 so not sure if it's a new property or it can be found on older versions.
This solved my problem! I'll leave a description for those who have some similar problems: Unity 2020.3.22f1 2D platformer project -I have a rigged sprite -3 main animations "Idle", "Run", "Jump" -When player dies I disable the main obj containing the sprite and the animator -If the animator is in "Run" or "Jump" state , it messes up the rig as if the default positions of the limbs targets are stuck in the last position they had before the animator got disabled -Keeping the animator state on disable basically let the animator switch smoothly between states on player respawn, solving the problem
Your answer
Follow this Question
Related Questions
Animation problem in unity2D 2 Answers
A better way to trigger animations. 0 Answers
Is possible Jump/advance to specific animation frame/time on the Animator Controller? 0 Answers
Blender FBX Import to Unity messes up animations 1 Answer
[Solvedf]Animator is not playing an AnimatorController 2 Answers