Search Unity

Question Animation not playing from script.

Discussion in 'Animation' started by donotprikel, Feb 5, 2023.

  1. donotprikel

    donotprikel

    Joined:
    Nov 14, 2022
    Posts:
    26
    I have been working on this for 2 hours and canot get it to work. I have asked my assistent and they game me a lot of options to try to fix but none of them worked. I have a script that plays an animation on mouse click so when i click button 0 on the mouse it should play an animation but it does not. But it plays fine in the animator window. I want it to play once everytime i click the left button on the mouse But it does not. No errors no warnings everything is how its suposted to be but its not playing. Can anyone help me. I even did debug log and it is registering the click but not playing the animation OK. I even did state machine index layer but still no luck. Heres the script. Screenshot 2023-02-05 110812.png
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MouseClickAnimation : MonoBehaviour
    4. {
    5.     public Animator anim;
    6.  
    7.     void Update()
    8.     {
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.             anim.Play("Fist", 0);
    12.         }
    13.     }
    14. }
    15.  
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Two things to check:

    Did you spell the animation state name correctly? (It's the name of the state in the animator, not the name of the animation file).

    Is the animation on layer 0 and not some other layer?

    That's all I can think of without seeing anything more.
     
  3. donotprikel

    donotprikel

    Joined:
    Nov 14, 2022
    Posts:
    26
    Yes thoes two are what my assistent gave me and they did not work
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Like most components, an Animator can't do it's job if it's disabled.
     
    Unrighteouss likes this.
  5. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    It would be pretty funny if that was the issue, but I think it's normal for the Animator to be unticked during animation preview. You can see the box is slightly blue, so I don't think it would be disabled at runtime. Something to check for sure though haha.

    Going to need more information then. A video demonstrating the issue and what the animator looks like during the issue would be ideal.
     
  6. donotprikel

    donotprikel

    Joined:
    Nov 14, 2022
    Posts:
    26
    Wait a second how you notice that i RILLY did not know HOW that actuly fixed it Theres apsolutly no way i could of missed that but i did THANK you so mutch
     
    Unrighteouss likes this.
  7. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Genius...
     
  8. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Pressing the preview button in the Animation window temporarily shows the Animator as being disabled:

    No Preview:
    No Preview.JPG

    Preview Ticked:
    Preview.JPG

    I am completely baffled that that was the actually issue haha.
     
  9. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    The window disables the Animator when you click Play and it stays disabled until you close the window. So maybe if it's actually disabling the Animator and not just faking it for the GUI, the disabled state could have been saved in the scene so it basically forgot that it needed to be re-enabled.
     
    Unrighteouss likes this.
  10. donotprikel

    donotprikel

    Joined:
    Nov 14, 2022
    Posts:
    26
    Yes right And sorry for waisting youre time with this post i just didnt notice it {ˢᵒʳʳʸ} :(
     
  11. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    No worries! No need to apologize, I've been stuck on simpler stuff in the past; happens to everyone.