Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

[Legacy & Mecanim] Instantiate + Play = No Animation ?!?

Discussion in 'Animation' started by manutoo, Jan 21, 2015.

  1. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    380
    Hello,

    when I instantiate an object and immediately call Animator.Play() , the animation doesn't work, it's like nothing is called.

    It's same for legacy Animation.Play() .
    Calling in the next frame with a coroutine works (ie: yield return null; Animation/Animator.Play(); ).
    For the legacy animation object, culling type is set to "Always animated" and for animator object to "Based on renderer", but it doesn't matter much as many objects aren't rendered on the next frame and they are still animated correctly if the call is made at that time.

    It happens during scene load (although I delay the loading with a coroutine, so it's technically not only on the loading frame that the problem arises) ; if I spawn the object later during the game, there's no need to delay the Play() by 1 frame.

    Anyone would have any idea about this ?
    What could possibly stop the Play() to work as expected ?
     
    Last edited: Jan 21, 2015
  2. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    380
    As it used to work before, I just browsed several backups and I found the reason.
    During the loading, I hide the object right after its init is done (and its parent, if it matters).
    So now I just set the hiding on the next frame, and in that case, the Play() is working correctly.
    I guess both anim systems need the object to remain active else they cancel the animation.
    It's a bit strange because I checked with the debugger, and after Play() is called, the AnimationClip contains the correct settings (ie: it's enabled and its weight is 1.0f). So hiding it right away resets these values to their initial state, or they are never used, even when the object is unhidden afterwards (I didn't check which case it was).
    It's kinda counter intuitive, no ..? :eek:
     
    Last edited: Jan 21, 2015
  3. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Activating/deactivating an object free all resources used by this object so this is expected
     
  4. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    380
    @Mecanim.Dev,
    nothing is freed, objects & their components are still there, checkable in the editor and by script, and everything is in the same state once re-enabled.
    Plus, I only disable the parent's GameObject, and disable the renderer of the GameObject with the animations, but I don't disable the GameObject itself nor its animation/animator component.
     
  5. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I mean when you deactivate an Animator or Animation, it does free all the internal resources and clear any animation that you did queue.

    I thought you were talking about enabling/disabling gameobject.

    So i'm not then what you mean by hidding
     
  6. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    380
    Ok, I got it, it's the internal stuff that is cleared.
    I just re-read the doc here : http://docs.unity3d.com/Manual/DeactivatingGameObjects.html , and deactivating the GO's parent has the same effect than deactivating the GO itself.
    So it would be good to add explanation about this internal clearing in the doc page I just linked.
    But maybe it'd be even better to apply the current frame anim before clearing anything ? I'm not really sure it'd be the best solution in all cases, though. With legacy system, I think I could have called Animation.Sample() by myself ; but it seems there's no equivalent for Mecanim, so I couldn't do it by myself with it.
    In all cases, a note in the documentation page of both Play() functions would be welcome. (ie: "Play() won't have any effect on the transform if the gameobject or its animation/animator component is deactivated during the same frame")
     
  7. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    380
    I come back with more thoughts.

    Actually, my initial fix was to delay the Play() to the following frame, but on that frame, the GO was still disabled (through its parent state) !
    So the Play() fails only on the Frame the disabling occurs. So even if there's some kind of freeing, the Play() still can store what it needs to store on the following frame, so the result is correct once everything is re-enabled.
    So it's really not an intuitive behavior. Maybe it comes from some sensed design, but that behaviour should really be outlined in the documentation.
     
  8. jakzun2011

    jakzun2011

    Joined:
    Jul 30, 2015
    Posts:
    65
    my project was going perfectly until unity said this:

    The AnimationClip 'idle' used by the Animation component 'Rat_AI' must be marked as Legacy.

    so, i made that particular animation a legacy and my timeline stopped working - all animations from all characters - even after i changed it back to generic and even deleted it

    even on my backups, none of my animation works - so far, i've lost an entire project that's shipping for christmas

    i can open new scenes and the timeline animations work - just not my project timeline - now, i get

    PlayableAsset returned a null Playable on Instantiate
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)

    -----------------------------------------

    ArgumentNullException: Argument cannot be null.
    Parameter name: The playable passed as an argument is null. Call IsValid() on playables to verify that they are valid before you use them.
    UnityEngine.Playables.PlayableHandle.SetPlayStateInternal (UnityEngine.Playables.PlayableHandle& playable, PlayState playState) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/PlayableBindings.gen.cs:242)
    UnityEngine.Playables.PlayableHandle.SetPlayState (PlayState value) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/PlayableBindings.gen.cs:143)
    UnityEngine.Playables.PlayableExtensions.SetPlayState[Playable] (Playable playable, PlayState value) (at C:/buildslave/unity/build/Runtime/Export/Director/PlayableExtensions.cs:28)
    UnityEngine.Timeline.ScheduleRuntimeClip.Create (UnityEngine.Timeline.TimelineClip clip, Playable clipPlayable, Playable parentMixer, Double startDelay, Double finishTail) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/ScheduleRuntimeClip.cs:57)
    UnityEngine.Timeline.ScheduleRuntimeClip..ctor (UnityEngine.Timeline.TimelineClip clip, Playable clipPlayable, Playable parentMixer, Double startDelay, Double finishTail) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/ScheduleRuntimeClip.cs:45)
    UnityEngine.Timeline.AudioTrack.OnCreatePlayableGraph (PlayableGraph graph, UnityEngine.GameObject go, UnityEngine.IntervalTree tree) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/Audio/AudioTrack.cs:49)
    UnityEngine.Timeline.TrackAsset.CreatePlayableGraph (PlayableGraph graph, UnityEngine.GameObject go, UnityEngine.IntervalTree tree) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/TrackAsset.cs:308)
    UnityEngine.Timeline.TimelinePlayable.CreatePlayableGraph (PlayableGraph graph, UnityEngine.Timeline.TrackAsset asset, UnityEngine.GameObject go, UnityEngine.IntervalTree tree) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/TimelinePlayable.cs:194)
    UnityEngine.Timeline.TimelinePlayable.CreateTrackPlayable (PlayableGraph graph, Playable timelinePlayable, UnityEngine.Timeline.TrackAsset track, UnityEngine.GameObject go, Boolean createOutputs) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/TimelinePlayable.cs:211)
    UnityEngine.Timeline.TimelinePlayable.CompileTrackList (PlayableGraph graph, Playable timelinePlayable, IEnumerable`1 tracks, UnityEngine.GameObject go, Boolean createOutputs) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/TimelinePlayable.cs:150)
    UnityEngine.Timeline.TimelinePlayable.Compile (PlayableGraph graph, Playable timelinePlayable, IList`1 tracks, UnityEngine.GameObject go, Boolean autoRebalance, Boolean createOutputs) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/TimelinePlayable.cs:129)
    UnityEngine.Timeline.TimelinePlayable.Create (PlayableGraph graph, IList`1 tracks, UnityEngine.GameObject go, Boolean autoRebalance, Boolean createOutputs) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/TimelinePlayable.cs:106)
    UnityEngine.Timeline.TimelineAsset.CreatePlayable (PlayableGraph graph, UnityEngine.GameObject go) (at C:/buildslave/unity/build/Extensions/Timeline/Runtime/TimelineAsset.cs:268)
    UnityEngine.Playables.PlayableAsset.Internal_CreatePlayable (UnityEngine.Playables.PlayableAsset asset, PlayableGraph graph, UnityEngine.GameObject go, IntPtr ptr) (at C:/buildslave/unity/build/Runtime/Export/Director/PlayableAsset.cs:54)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)