Play random sound from a selection?

First of all, I know little about audio, I know how to trigger it and correctly use it in 3D space…that’s about it.

I am looking to be able to use this single line of code (or similar):

gameObject.GetComponent<AudioSource>().Play();

To play not only the sound on that AudioSource component, but to randomly select 1 from a list of sounds (there are 7).

I am not sure whether this should be in Scripting or in Audio, so I hope this is the right place.

Make an array or a list of audioclips and load them into the audiosource.

You should also get the AudioSource on Start(), GetComponent() is a slow method.

audioSource.clip = clipList[Random.Range(0, clipList.Length)];
audioSource.Play();

Random.Range returns a random integer number between min [inclusive] and max [exclusive] , so you don’t use -1

I didn’t know that, thanks for telling !