Search Unity

Why when you disable then re-enable the same game object the animation is restarted?

Discussion in 'Animation' started by BenoitFreslon, Sep 16, 2015.

  1. BenoitFreslon

    BenoitFreslon

    Joined:
    Jan 16, 2013
    Posts:
    166
    Hello,

    I create a simple project and the put one animated (Unity Animation) cube If I disable the Cube the animation is paused in the Animator but If i re-enable the GameObject the animation restart from the beginning.

    I want to pause the current animation & hide the gameobject then resume the same animation.

    Thanks.

    Donwload the unitypackage:
    https://dl.dropboxusercontent.com/u/34379764/Animation_restart.unitypackage
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    Yes, when you disable an Animation component, it releases its memory. When it gets re-enabled, it restarts everything. You'll have to save the state on your own and then resume that state right after re-enabling the Animator.
     
  3. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    I find this behavior counter-intuitive. If I wanted to release the memory I would delete the game object. How should one disable object temporarily without some hacks?
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,706
    Mecanim.Dev wrote more about it in this post and this post. In the second link, another user posted a wrapper script that helps address this.
     
  5. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    Thanks for the links. So I'm not the only one :) But it looks like they're going to support "persistent" state in the future.
     
  6. BornGodsGame

    BornGodsGame

    Joined:
    Jun 28, 2014
    Posts:
    587
    You could also just disable the mesh renderer component on the cube game object without disabling the entire cube. ( I think). Basically the cube keeps spinning, but is now invisible.