MrRobot9
MrRobot9

Reputation: 2684

Unity3d : Access the OnClick parameters of Button in a script

enter image description here

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

Answers (1)

You're better off writing this as C# code

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

Related Questions