Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Neurus_Ex · Jan 16, 2017 at 08:14 PM · animatoranimator controllerenable and disable script

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Neurus_Ex · Jan 17, 2017 at 12:48 PM 0
Share

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.

avatar image ASPePeX Neurus_Ex · Jan 17, 2017 at 03:55 PM 0
Share

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.

avatar image Neurus_Ex ASPePeX · Jan 17, 2017 at 04:02 PM 0
Share

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.

avatar image
11

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shimanshu12596 · Sep 01, 2020 at 04:57 PM 0
Share

Thank you so much! It worked like a charm.

avatar image forewar · Nov 09, 2020 at 06:59 PM 0
Share

You made my day, thanks a lot!

avatar image hjine01 · Nov 26, 2020 at 09:03 AM 0
Share

Thank you very much!!!!!!!!

avatar image Shippety · Jan 05, 2021 at 07:28 PM 0
Share

You just saved me so much time, thank you kindly!!

avatar image unity_ftPO-4l1uh4FkQ · Feb 01 at 04:31 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Controlling position through animator and code simultaneously? 0 Answers

why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers

Slow down an animation from Script - C# 0 Answers

Unity 5.6 Animator.Rebind doesn't work - Animator doesn't reset on disable & re-enable gameobject 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges