tim
tim

Reputation: 1482

Why the AudioSource does not play?

This is the setting

enter image description here

When I GetComponent<AudioSource>().Play(), there is no sound, why?

When I check the play on awake, the sound is played. Why?

Upvotes: 0

Views: 1725

Answers (1)

Programmer
Programmer

Reputation: 125245

The problem is from ElementControl.cs. You are always destroying the GameObject before the sound even gets to play.

SOLUTION 1:

The simple fix for you is to find any code like:

Destroy(this.gameObject); 

in your scene then replace it with Destroy(this.gameObject, 5);. The 5 will make it wait for 5 seconds before destroying the GameObject and the sound may have finished playing by that time. There are about three Destroy(this.gameObject); that must be changed. Look for them.

SOLUTION 2 (Recommended):

Change your void PlayClickAudio() function to Coroutine then replace anycode that calls it with StartCoroutine. Inside it, play the sound then use audio.isPlaying to wait until the sound is has finished playing. Do other stuff you in the OnPointerClick function then you can destroy the GameObject with Destroy(this.gameObject);.Also the original code uses audio.Play(44100) which will make it delay before playing. Change it to audio.Play().

Here is the new script with solution 2: http://pastebin.com/m1tbVj9k

Upvotes: 1

Related Questions