AnimatorStateInfo.normalizedTime always returns 0
I'm trying to disable a gameObject with an animator attached, then re-enable it in the same position at the same point of animation. Right now, it re-enables and automatically plays starting from the beginning of the animation. Here's the chunk of code I'm using to record the normalized time:
// Disable target
m_targetAnimations = m_target.GetComponentsInChildren<Animator>();
m_targetAnimationTimes = new List<float>();
for (int i=0, j=m_targetAnimations.Length; i < j; i++)
{
Animator anim = m_targetAnimations[i];
AnimatorStateInfo asi = anim.GetNextAnimatorStateInfo(0);
m_targetAnimationTimes.Add(asi.normalizedTime);
Debug.Log(asi.normalizedTime);
}
m_target.SetActive(false);
The Debug.Log I placed inside that loop always returns 0, no matter what point of the animation the target is in. If I hard-code and add ".5f" to m_targetAnimationTimes, the animation resumes at the half-way point every time, so I know recording the normalizedTime is where the problem lies.
Help would be greatly appreciated, I can't seem to find any way around this or any questions that are asking the same problem. Any ideas? Thanks!
Answer by gnp89 · Feb 24, 2017 at 07:19 PM
Are those children active? Seems like normalizedTime is 0 when gameobject is not active. Maybe those children under "target" where disabled before you execute this?
Answer by OgnjenSelver · Oct 07, 2021 at 12:10 PM
GetCurrentAnimatorStateInfo(0).normalized time returned me like 2, 5,9 and 20 seconds when i start app till i trigger animation in my 4 trys(i wait some time and triggered it randomly on touch). I used if ,,(animator.GetCurrentAnimatorStateInfo(0).normalizedTime>1){some code to be run if animation was on/still on},, and it worked. (Bcs Game object on which is script and animator existed from start in scene). But at some point i had to trigger 2nd animation, and way i did it, it wasnt same as first. When i do something object gets SetActive(true) , this object has srcipt and animator on, and this is were problem happend. Because i just activated object it is in scene for 0 seconds -script starts-animation plays- but this condtion ,,if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime>1),, is FALSE. Because ,,Debug.Log(animator.GetCurrentAnimatorStateInfo(0).normalizedTime),, returned me 0. So it seems ,,animator.GetCurrentAnimatorStateInfo(0).normalizedTime,, returns time since ,,life,, of object started in scene till this animation state ( for me animation state started as soon as object was active).
Your answer
![](https://koobas.hobune.stream/wayback/20220612042620im_/https://answers.unity.com/themes/thub/images/avi.jpg)