Is having multiple audio sources on one object like this bad practice?

Just got to the section of the course for Multiple Audio Clips and attempted something dealing with audio before reaching this section. Just curious, is it bad practice/efficiency to have multiple audio sources for different sounds that I referenced in a script? Also, would I need two audio source controls if I were to play background music and in-game effects at the same time?

I drag and dropped the Audio Sources to the references and played them without issues, but noticed they were all named Cube and I could not tell the difference. Is this because it is under the same object?

1 Like

Hi Karil,

Depending on your ideas, one AudioSource could be sufficient. With the PlayOneShot method, you could play multiple sounds.

No, you do not need two audio sources if the player does not have the option to adjust the volume. One even if there was such an option in your game, you would not need them. However, in this particular case, you would rather want to use two AudioSource components and the AudioMixer because, otherwise, you would have to write your own solution.

Components do not have a name. If you want to get a specific component via code, there is (often) no way to distinguish between components of the same type if they are attached to the same game object. The solution would be to expose variables with [SerializeFields] and assign the reference to them manually.

Regarding the performance of your approach, this game is so small that it is fairly unlikely that you’ll notice any significant difference if you use a bunch of AudioSources in your scene instead of one.

Did this answer your questions?


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms