Reputation: 2684
I have a button and I have attached another script "PlaySound
" for this button, which plays audio on button click. As soon as the audio gets completed, I want to stop the animation which I am triggering here in OnClick
of Button
. How do I access the Ellen
gameObject and 0
parameter which I set in the Inspector window (check the picture) in the PlaySound
script
Upvotes: 1
Views: 2350
Reputation: 15951
What you have now functionally looks like this:
public class SomeScript : MonoBehaviour {
public Animator ellen;
void Start() {
GetComponent<Button>().onClick.AddListener(delegate() {
ellen.SetTrigger(0);
});
}
}
Where this script is attached to your button and the Ellen gameobject's Animator
component is assigned to the ellen
field.
If you change it like so:
public class SomeScript : MonoBehaviour {
public Animator ellen;
public float audioDuration;
void Start() {
GetComponent<Button>().onClick.AddListener(delegate() {
StartCoroutine(TriggerAnimation());
});
}
IEnumerator TriggerAnimation() {
ellen.SetTrigger(0);
yield return new WaitForSeconds(audioDuration);
ellen.ResetTrigger(0); //or however you want to return to the idle state
}
}
Now you just need to supply a duration for the audio clip. There may also be a way to query the audio source's current play status, but I don't have enough information about your project to be able to hash that code out in a functional way.
Upvotes: 2