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. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Bug Animation can't enable/disable GameObject

Discussion in 'Animation' started by Carlfiii, Sep 28, 2022.

  1. Carlfiii

    Carlfiii

    Joined:
    Feb 4, 2020
    Posts:
    20
    I'm using a very basic animation to create a transition between rounds for my 2D mobile game.

    Goal:
    - Once a round ends, play a short animation which then leads to the next round.
    - The ChangeRound animation is about 1 second long. It enables the UI image at the beginning, and disables it at the end of the animation.
    - It also has an AnimationEvent called OnChangeRound() that tells the game to start the next round.

    Problem:
    - Animation is playing, but the enable/disable gameobject property is not triggering.
    - Additionally, the event is also not triggering.
    - During gameplay, I am unable to manually toggle the gameobject's active state on/off. It is not being controlled by a script, and the animation is not playing at the time.

    What I've done to fix it:
    - Made sure I wasn't on the animation tab's preview so it wouldn't lock it
    - Moved the Animation Event a few frames before the end. I read online that animation events at the very last frame don't trigger (Even though I've done it before)
    - Viewed the Animator window and know for a fact that the animation is playing, I can see the transition and status bar on the node

    Script:
    Code (CSharp):
    1. public void ChangeRound()
    2.     {
    3.         Debug.Log("Starting change round");
    4.         ClearLog();
    5.         _animator.Play("ChangeRound");
    6.     }
    7.     public void OnChangeRound()
    8.     {
    9.         Debug.Log("Changed Round");
    10.         GameManager.instance.StartGame();
    11.     }
    Animator:
    - Sits in an Idle state by default, doesn't do anything
    - I use animator.Play() instead of trigger/bool transitions since there are no other animations ever playing



    Heirarchy:
    - The HUDScripter contains the script and Animator component
    - I'm just trying to enable/disable the Erasers gameobject. Currently its a UI image w/ a white square for testing purposes


    Animation:
    - Simple animation clip. Works fine when previewing it
    - Enables the gameobject at the beginning, then disables it at the end.
    - Will play the OnChangeRound() event at the animation event



    I am completely baffled that this is not working. I've made plenty of basic animations before and this seems like such a straightforward function that completely refuses to work. I imagine I'm missing something very simple but I've spent hours on this with no solution. Any help would be appreciated

    Update:
    - I put the animation event in the middle of the animation rather than near the end and now it triggers, but the isActive properties are still not triggering. So I know for sure that the animation is being triggered and running. Although I dont understand why the last 10-15 frames of the animation don't allow animation events to play?
     
    Last edited: Sep 28, 2022
  2. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    121
    Here you can disable it through the Animator itself, just make a smooth disappearance of your object through the alpha channel if it uses color and turn it off, and yes, most likely the animator should be above the object in the hierarchy so that it cannot disable itself.
     
  3. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    121
    Alternatively, you can try to make the object a child of the animator, and disable this object when it is required in this case at the end of the animation