How to play a random audio clip from an array in C#?

Hello!

So I’ve made an audio array like this:

public AudioClip[] shoot;

And placed two audio clips inside of it. Now my question is; how do I play a random audio clip from that array? All the methods I’ve seen online used obsolete code.

Well. Only one method I know.

But first assign this variable

public AudioSource source;

Then add audio source that play gunshot sound.

And then use this code to random Shoot clip.

source.clip = shoot[Random.Range(0, shoot.Length];
source.Play(): //Or .PlayOneShot();

If you have

using System;

You have to add UnityEngine in front of Random

source.clip = shoot[UnityEngine.Random.Range(0, shoot.Length];