Animation won't play when clicking button

I have an animation which I want to get played – it’s a black ”curtain” image that’s supposed to fade in from 0 alpha to 1 to make the scene dark, resulting in a ”fade out” scene effect.

The animation works fine when I play it manually in Unity, but when I attach the Panel (which has the animation) to a Button and adding a ”On Click” parameter, dragging the Panel in to the object box and then choosing ”Animator” > ”Play (string)” and naming the animation I want to play in the box underneath, doesn’t cut it – the animation isn’t played when I click ”Play” in unity and click the button that should trigger the animation.

I have checked through things and I can’t find what’s wrong. :frowning: Any ideas?

If you’re using an Animator, you ought to add an AnimatorController, and a trigger that will start the fade animation.

Then, on your button, rather than calling Play(string), call SetTrigger(string), and the AnimatorController will take care of the rest.

If you’re using an Animation instead of an Animator, then what you outlined should be correct, so you must have missed a step in the execution. Post screenshots of the inspector settings for those things so we can take a look.

Seems I’ve found the problem.

In the Hierarchy tab I had the button placed as a child to the black image curtain that’s animated to fade in and out. When the button was placed there the animation wasn’t triggered, but as soon as I moved out the button so it’s next to the curtain game object (so to speak) the animation (and scene switching) works fine when clicking the button!

Not sure if this is the intended behaviour in Unity or if it would be considered a bug.