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.
Answer by Toon_Werawat · Mar 27, 2016 at 11:46 AM
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];
When I try that I get this error:
Assets/Scripts/Shooting/BulletSpawn.cs(21,54): error CS1061: Type UnityEngine.AudioClip' does not contain a definition for
Play' and no extension method Play' of type
UnityEngine.AudioClip' could be found (are you missing a using directive or an assembly reference?)
Your answer
Follow this Question
Related Questions
GetSpectrumData Why last element in array allways is zero? 0 Answers
Soundarray for player is not working properly 1 Answer
Second AudioClip won't play 0 Answers
[Tanks Tutorial] The "engine driving" AudioClip doesn't start 1 Answer
Audio Source does not contain definition for any of the Play functions... 2 Answers