Are there any advantages/disadvantages in the different approaches to playing audio?

In the earlier steps we created an “Audio Source” component for the ball, and used the script (method: PlayOneShot) to tell the component to play a clip.

I was expecting the same behavior for blocks, but we didn’t even need to create the audio source component and directly called for “AudioSource.PlayClipAtPoint”.

i was just wondering, since you showed us 2 ways to play audio 1) Using Audio source component, 2)Using AudioSource.Play method in script, are there any advantages or disadvantages of using one or the other? Which one should i use in general?

Hello Bing,

Regarding you query I would lean towards, where-ever possible, creating/destroying as few objects as possible, as this would tend to be more performant.

In the Block Breaker project, part of the issue was that if the block was responsible for playing the audio, the audio would stop when the block was destroyed, cutting the sound effect off early. This didn’t apply to the ball because the ball had no audio effect when it was destroyed, although if one was added then the same issue would apply.

An alternative approach could be to have a GameObject with an AudioSource component on it and then communicate between objects, e.g the block says, “Hey, I’m being destroyed, play a clip”, the SoundEffectSystem would then play the audio clip. This would remove the issue from the block and also prevent he creation/destruction of multiple GameObjects unnecessarily.

Some consideration and testing would be involved in the above as you will find there will be a limit to the number of simultaneous audio clips that can be played at anyone time, this issue would still be the case using the methods demonstrated in the project. It is unlikely in a game like Block Breaker you would hit any limit though, as some audio clips would have finished before others were due to play, its unlikely 200 blocks for example in a scene would all be destroyed at the same time.

I hope the above is of use :slight_smile:

2 Likes

Privacy & Terms