Audio for a toolbar?

Hello. When using a GUI toolbar, how do you play an audio clip when one of the options is selected(I know how to do this for a button, but when I try to put it in for the toolbar, it plays continuously because it is being updated)? Thanks.

You need an AudioSource component, on the same GameObject makes it simpler to manage.
Then you need to catch when the selection changes. You can do that by having two variables, one for the previous state, and one for the new state. Whenever the new state is different from the previous, you play the sound and set the previous to match the new.

Pseudo code (UnityScript) :


function Update () {
    if (_previous != _new) {
    this.GetComponent(AudioSource).Play();
    _previous = _new;
}
}

There may be better ways to do it, but this one worked for me…

hope this helps