Search Unity

Question Animation cannot be played

Discussion in 'Animation' started by martincz26, Mar 10, 2023.

  1. martincz26

    martincz26

    Joined:
    Jan 28, 2021
    Posts:
    13
    Hi, I'm working on a simple 2D platformer where the player collects various items, and I'm struggling with a simple problem that I don't know how to solve.

    The moment the player picks up an object, the animation of picking up the object should play, which is placed on the object. It's a sort of teleportation effect.

    I then have an Item_Collector script on the player that takes care of this. So as soon as the player touches the item the animation should be activated, the trigger collected is a condition of the animation. And when the animation is over, the item is removed from the scene. And the player is awarded some points, depending on what he has collected.


    Code (CSharp):
    1.  
    2. Animator anim;
    3.  
    4.  
    5. [SerializeField] private GameObject collectibleGameObject;
    6.  
    7. private void star()
    8. {
    9.     anim = collectibleGameObject.GetComponent<Animator>();
    10. }
    11.  
    12.  private void OnTriggerEnter2D(Collider2D collision)
    13.     {
    14. if (collision.gameObject.layer == LayerMask.NameToLayer("Collectible"))
    15.         {
    16.             if (collision.gameObject.CompareTag("SomeItem"))
    17.             {
    18.                 collectionSoundEffect.Play();
    19.                 anim.SetTrigger("collected");
    20.                 Destroy(collision.gameObject, anim.GetCurrentAnimatorStateInfo(0).length + 0);
    21.                ....
    22.             }

    My code is simple, first I initialize the animator, and the gameobject.

    Then in the start method, I initialize the animator of the specific gameobject and then I just play the animation if the object is collected. Then in the destroy method, I wait for the animation to finish, and then remove the object.


    And here I get an error, or rather a warning: animator is not playing an AnimatorController.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Line 7 should be
    void Start()
    , that code isn't running.
     
  3. martincz26

    martincz26

    Joined:
    Jan 28, 2021
    Posts:
    13
    How did it get there.... Anyway, it's not in the code I got it right plus such an entry would throw me another error
     
  4. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Look at this post for why you're getting that error: https://forum.unity.com/threads/ani...nityengine-animator-settrigger-string.465254/

    It's likely that the gameobject containing the animator isn't active when you try referencing it in code.

    If you're still struggling, please repost your code in a way that is accurate and readable.
     
    Last edited: Mar 12, 2023
  5. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    983
    You are starting the animation and then immediately destroying the object. There will be no delay between those two lines of code (aside from whatever calculations are needed to setup the animator state). You need to actually wait for the animation to complete before destroying it. As a simple approach, consider looking into Animation Events. You cold also consider writing a State Machine Behaviour that the animation flows into after it completes. A couroutine could be used to delay the destruction of the object for a length of time could also be used.

    There are many other ways that you could setup operations that happen over time to either check the current state of the animation or time it out in some way to know when to call destroy but that's probably a little too much to worry about just yet.


    Nevermind, I just realized you are using the delayed version of destroy. Time for more coffee :p
     
  6. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    983
    Although I do know what is wrong anyway. When you tell an animator to play an animation it does not update its internal state until the next animation update cycle of the frame you are in. This means the time you are passing as the delay is not the time of the animation you just set but rather the time of the last state just before you set it. You can manually 'pump' the animator by calling its Update() method just after setting the animation to play and it will then have the updated info.

    Seriously, I think this is like the third or fourth post this week where this has come up lol