I have multiple Audio Sources on a game object in my game, and they all just default to the same name, “Audio Source”. The audio clips I have attached to them all have unique names of course, but in my scripts I just have to refer to them as variable names I create, without knowing (other then through trial and error), which variable goes with which Audio Source. This would be fine if there were only a few sources, but I am doing a music game, so there are a LOT of them attached to one object. Is there any way to name or tag an audio source?
I suggest you to have only an AudioSource on a single GameObject and a vector of SoundClip.
In your script you can assign the proper AudioClip to the AudioSource and play it at need.
If you need to play more then one clip at the same time you can use the PlayOneShot function or simply attach an AudioSource to another GameObject.
There are some ways to deal with multiple audio sources on the same objects, but none is all that great. The “easiest” is probably to work with the AudioSource[ ] array, which every game object has. (“audio” is just a shortcut to element 0 of that array.) The elements in the array are in the same order as they are on your game object, from top to bottom in the Inspector. That’s still not great, so I really don’t recommend having more than one per game object if you want clarity in the Editor.
“That’s still not great, so I really don’t recommend having more than one per game object if you want clarity in the Editor.”
Thanks for the feedback! Do you mean that I could just make a large number of game objects in the game that have the sole purpose of just holding one audio source each?
Yes indeed. If you need a bunch of clips to be loaded simultaneously, that’s the way to do it. Just parent them to another object to eliminate clutter in the Hierarchy.
You may still want to use an array of audio sources on the parent, and populate it with the children, though, for code clarity.