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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Crash on AnimationClip.SampleAnimation()

Discussion in 'Animation' started by Hooshangi-reza, May 10, 2015.

  1. Hooshangi-reza

    Hooshangi-reza

    Joined:
    Dec 9, 2013
    Posts:
    6
    I'm using AnimationClip.SampleAnimation(GameObject, float) on Unity 5 and it works fine in Editor but when I build the game it crashes on executing that line of code!
    Doe's anyone have any idea about this bug?
    this is my code:

    Code (CSharp):
    1. public class EntityAnimationHandleTesterComponent : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private AnimationClip clip_;
    5.  
    6.     void Update()
    7.     {
    8.         if (Input.GetKeyDown(KeyCode.A))
    9.         {
    10.             Debug.LogWarning("Before");
    11.             clip_.SampleAnimation(gameObject, 1f);
    12.             Debug.LogWarning("After");
    13.         }
    14.  
    15.     }
    16. }
    clip_ is assigned in inspector window and the gameobject contains an animator that have clip in it. When I press the 'A' button in build, the game crashes! the last log in crash log is 'Before' and this is it's stack trace :

    ========== OUTPUTING STACK TRACE ==================

    0146D582 (Build) UnityEngine::Animation::CreateAnimatorGenericBindings
    014AE6A5 (Build) Animator::SetupControllerMecanimDataSet
    014AF1CF (Build) Animator::Sample
    014C3C19 (Build) SampleAnimation
    01340815 (Build) AnimationClip_CUSTOM_SampleAnimation
    060F5302 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.AnimationClip:SampleAnimation (UnityEngine.GameObject,single)
    060F4D82 (Mono JIT Code) [D:\rza\Workspace\Tests\SampleAnimationTest\New Unity Project\Assets\AnimationHandleTest\EntityAnimationHandleTesterComponent.cs:26] EntityAnimationHandleTesterComponent:Update ()
    060DAAAF (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    100F054D (mono) mono_set_defaults
    1005D812 (mono) mono_runtime_invoke
    0113AABE (Build) scripting_method_invoke
    0120100C (Build) ScriptingInvocationNoArgs::Invoke
    01200E15 (Build) ScriptingInvocation::Invoke
    01128B4A (Build) MonoBehaviour::CallMethodIfAvailable
    0112A788 (Build) MonoBehaviour::Update
    011B9D21 (Build) PlayerLoop
    0126D6E5 (Build) PlayerMainWndProc
    0126F601 (Build) PlayerWinMain
    015F48A8 (Build) WinMain
    0162BF99 (Build) _get_printf_count_output
    75A7919F (KERNEL32) BaseThreadInitThunk
    77A3A8CB (ntdll) RtlInitializeExceptionChain
    77A3A8A1 (ntdll) RtlInitializeExceptionChain

    ========== END OF STACKTRACE ===========

    **** Crash! ****
     
  2. alexhy

    alexhy

    Joined:
    Nov 21, 2013
    Posts:
    26
    me too!
     
  3. iko79

    iko79

    Joined:
    Jan 21, 2013
    Posts:
    45
    Same here. Only when I call
    Code (CSharp):
    1. this.clip.SampleAnimation( this.gameObject, this.currentAnimationPos );
    in my script -- when I remove this line, everything runs like a charm. This is quite frustrating since I depend on this method and I don't know what a workaround would be. What I need is a possibility for putting my GameObject to position X along a predefined 3d animation trail. Any ideas?

    btw: I connected with Visual Studio and got the crash inside of UnityEngine::Animation::CreateAnimatorGenericBindings. Here's the stack trace, in case this helps:

     
    Last edited: May 21, 2015
  4. Hooshangi-reza

    Hooshangi-reza

    Joined:
    Dec 9, 2013
    Posts:
    6
    @iko79 Do you use Unity 5?
     
  5. iko79

    iko79

    Joined:
    Jan 21, 2013
    Posts:
    45
    I do. Unity 5.0.1f1. However, I found a way to work around the issue. In my case, all I need is the animation clip itself. I'm not at all familiar with the animation system, to be honest I find it absolutely confusing, so I actually thought I have to have an Animator component in my GameObject, with it's Controller field set to my AnimatorController, which in turn is containing the AnimationClip. So in my script I tried to access the clip as follows:
    Code (CSharp):
    1. this.clip = this.animator.runtimeAnimatorController.animationClips[0];
    which worked in the editor but caused the crash in the build. Since the only thing I have to do is sampling the animation in order to place the GameObject on a position on the curve, I tried to delete all that stuff. So I deleted the AnimatorController and removed the Animator component, instead I put a public AnimationClip field in my script and assigned the clip directly in the Inspector, by dragging it from the Assets. I'm not sure if this solves your issue, but it worked for me since I only needed the clip and it seems to work without all the other stuff. Hope it helps.
     
    theANMATOR2b likes this.
  6. Hooshangi-reza

    Hooshangi-reza

    Joined:
    Dec 9, 2013
    Posts:
    6
    I did the same. I have some public AnimationClip fields in my script and assign the clips in editor so this shouldn't be the source of problem.So I tested your removing AnimatorComponent. It doesn't crash but the problem is it's not working anymore!
    It seems AnimationClip.SampleAnimation() doesn't work in the absence of AnimatorComponent.
    Does it work for you?
     
  7. iko79

    iko79

    Joined:
    Jan 21, 2013
    Posts:
    45
    Yes it does. You doublechecked your clip and all your keyframes were still present? Because I noticed my animation getting lost at random occasions. No idea what caused this, I did nothing, really, but suddenly the animation clip was empty. I had to restore the files by reverting changes with GIT several times (luckily I'm using versioning), otherwise I would have had to manually rebuild the whole animation. This animation system is a mess, is my humble opinion.
     
  8. Hooshangi-reza

    Hooshangi-reza

    Joined:
    Dec 9, 2013
    Posts:
    6
    I checked a lot of cases to understand the difference between my case and yours and I think found it. There are 2 types of animation curve in Unity. normal curves like position and rotation and other value fields use FloatCurve but some special things like Sprites use ReferenceValueCurve. Removing AnimatorComponent solves this bug for FloatCurves but it's not working for ReferenceValueCurves!
    This is bad news for me since my most common use of this system is for Sprites.
     
  9. ciprian_facerig

    ciprian_facerig

    Joined:
    Mar 25, 2015
    Posts:
    34
    We've encountered this issue as well, in iOS and Android builds. Works ok inside the Windows and Mac Editors though. It would be nice to know if they know of this crash and if/when they're gonna fix it !
     
  10. Hooshangi-reza

    Hooshangi-reza

    Joined:
    Dec 9, 2013
    Posts:
    6
    The bug is fixed in Unity 5.2
     
  11. ciprian_facerig

    ciprian_facerig

    Joined:
    Mar 25, 2015
    Posts:
    34
    Yep, I can confirm 5.2 fixed it for us too, at least on iOS where I tested :)