Play Audio

Hello Fellas,

I have a gameobject with a Audio attached. If i choose “play on awake” the sound goes perfectly.

The problem is that i must trigger the sound when i click in a object. This code is not working:

//Tocando um som
GameObject.FindWithTag(“Som”+perguntaAtual).audio.Play();

This line find the gameobject, don’t gives any error, but don’t play the sound.

Any ideas?

Insert audio clip as a parameter of Play() function.

The AudioSource is what you’re using to play the sound.

Either by using [*Play*](https://docs.unity3d.com/Documentation/ScriptReference/AudioSource.Play.html) or [*PlayOneShot*](https://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayOneShot.html).

In the case of Play you set the clip first:

// This can be done at Start
AudioSource myAudioSource = GameObject.FindWithTag("Som"+perguntaAtual).audio;
myAudioSource.clip = myAudioClip;

// This can be done wherever you want it to play
myAudioSource.Play();

Using PlayOneShot you just pass through the clip to the function.

What’s myAudioClip?

This is your actual sound, your audioClip ( https://docs.unity3d.com/Documentation/ScriptReference/AudioClip.html ).

AudioClip myAudioClip;