Animation cannot enable/disable gameobject, and prevents me from manually doing so.

Despite this being a simple problem, its going to be a large post.

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 for debugging:

  • Made sure I wasn’t on the animation tab’s preview so it wouldn’t lock it

  • The AnimationEvent plays when I push the event keyframe farther from the end. When its about 1-5 frames near the end it never works, but if its at least 15+ frames from the end the event plays

  • Animator Window shows that the animation is definitely playing, and the previous point also lets me know for sure that the animation is playing.

  • Debug statements that prove all the correct functions are firing

  • Removed any layers from the Animator to ensure its always playing the correct animation


Script:

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
    200241-animator.png


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
    200242-objectheirarchy.png


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
    Website wouldn’t allow me to upload 3rd picture


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

I guess problem is that animation is cut off in transition between states. Insted of animation event try to attatch StateMachineBehaviour script to ChangeRaound animation block, and in OnStateExit method call your event or function what you want.

Greetings @ScuffedCD

If you are happy for the animation always to be a fixed length, you can use a co-routine to wait for a fixed period then execute your code.

IEnumerator MyDelay(float delayTime)
{
    yield return new WaitForSeconds(delayTime);
    // Your code here
}

Just start the co-routine at the same time as starting your animation: StartCoroutine(MyDelay(1));

You can also use Invoke() but I find coroutines a little more flexible and I’m not fond of hard coding method names in a string.