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. :( Any ideas?
Answer by FortisVenaliter · May 20, 2016 at 02:23 PM
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.
Thanks so much for your reply! :)
I'm not sure, but it seems to me I'm using both an Animator and an Animation- :-| At least I have a tab called ”Animator” and a tab called ”Animation” in Unity and both contains stuff. :) As you can tell I'm pretty new to all this still…
Anyway, I'm attaching three screenshots – one of the inspector for the ”panel” (the black canvas) and one of the inspector for the button (the one I want to trigger the animation when being clicked on). Also a third screenshot with the Animation and the Animator tabs visible.
I tried to change the ”Play(string”) to ”SetTrigger(string)” but no go. I guess there's something more missing…
Had some trouble attaching the screenshots here, so here are links to them on my Dropbox:
Inspector view when Panel (black canvas) selected: https://dl.dropboxusercontent.com/u/2096906/Pictures/Unity/Panel-Black_Curtain.png
Inspector view when button is selected: https://dl.dropboxusercontent.com/u/2096906/Pictures/Unity/Button.png
View of entire Unity window with Animator and Animation tabs visible: https://dl.dropboxusercontent.com/u/2096906/Pictures/Unity/Animator_and_animation.png
You should not be using both. And don't look at the tabs, look at the components attached to the GameObject.
I recommend you pick one, and watch some tutorials to fully understand how it works before proceeding.
Uhm, okay.
What I've done is to follow the guide here: http://www.thegamecontriver.com/2014/09/add-fade-between-scenes-unity-46.html
I just did it again the other day – followed that guide in a new project and there both the fade in and fade out works fine. So there is something I have done different in my ”real” project.
Anyway, I'll try to look at some tutorials as you suggested and see if I can get my head around things.
Thanks again!
Answer by staraffinity · Jun 04, 2016 at 11:43 PM
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.
bro i have a problem when i click right mouse button my animation does not work but however the bool gets checked. here is the code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FiringPistol : MonoBehaviour { public bool isAi$$anonymous$$g = false; public GameObject thePlayer;
void Update()
{
if (Input.GetMouseButton(1))
{
isAi$$anonymous$$g = true;
thePlayer.GetComponent<Animation>().Play("Pistol");
}
else
{
isAi$$anonymous$$g = false;
//thePlayer.GetComponent<Animation>().Play("Idle");
}
}
}
Please help me bro