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. Dismiss Notice

Question Can't get animations to play.

Discussion in 'Scripting' started by EO-Altacc, Mar 13, 2023.

  1. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    I'm having trouble getting animation to play with both the Animation and Animator components and am wondering if my scripting is incorrect.

    I have an FBX which was exported from Blender with several Animation Clips:
    upload_2023-3-13_11-1-48.png

    I then made a refab from the FBX file, because I needed to attach some scripts to it, and I cant add components onto a imported model.

    I added a Animation component and under its animations list, I placed the 2 animations:
    upload_2023-3-13_11-15-7.png

    In a script I have the following to play the animation:

    Code (CSharp):
    1. Debug.Log("Playing Clip: " + _int.Clip_Name + "|" + (anim.GetClip(_int.Clip_Name) != null).ToString());
    2.             anim.clip = anim.GetClip(_int.Clip_Name);
    3.             anim.Play();
    However the animations never play. I've checked that the clip is found using a debug message Debug.Log((anim.getclip("clip name") != null).tostring()). I've also checked that the animations work from the preview, and from just directly putting the animation clip into the clip parameters of the Animation Component and setting it to auto play on start, and it plays properly. I've also tried switching to the newer Animator component, adding the new animation clips into it and trying that way. Nothing I do seem to work.

    Here's some settings on my imported model - not sure if it has anything to do with it.

    upload_2023-3-13_11-9-55.png upload_2023-3-13_11-10-14.png upload_2023-3-13_11-11-13.png

    Anyone got ideas what I'm doing wrong. I'm using Unity 2021.13.20. Note that I had to put the rig to Legacy Mode because Unity throws me an error telling me I have to if I dont.
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Do you have an animation controller and an animation object attached to the prefab?
     
  3. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    Can you try to drag and drop the animation of the fbx to an empty GameObject and tell us what the result is when you start play mode?
     
  4. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    When i use the Animation component, the component is placed on the Prefab. When I tried using the Animator component, I made a Animation Controller, converted the model to Humanoid, and placed both into the slots of the Animator is placed on the Prefab.

    I have to mention that the animation from blender are NLA Strips. Not sure if that has anything to do with it.
     
  5. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    That just creates a Animator on the object. The Animator method also doesn't work for me.
     
  6. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    So you tried it with legacy mode, right? Can you give it a try to drag and drop the animation to an empty GameObject with unchecked lagacy mode?
     
  7. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    In Legacy mode, it creates the Animation component and adds the animations to the animations list. Seems like what I was doing before

    Edit: Without Legacy mode, I simply get an error telling me to put it in legacy mode. It still doesn't play.

    The Animation does play if I place the clip into the Animation slot and I set it to play Automatically. So I don't think its the animation itself.
     
    Last edited: Mar 13, 2023
  8. EO-Altacc

    EO-Altacc

    Joined:
    Feb 19, 2019
    Posts:
    83
    Problem Fixed.

    The cause was the prefab I made from the object. Apparently I can't call animations on a prefab made from the importer model and only on the model itself. I moved all the scripts to a separate object and it worked. :)