Question by
Eman91 · May 13, 2016 at 09:54 PM ·
c#unity 5monodevelop
How do I create an array for sounds?
Hi, I'm a rookie Unity user and I'm currently trying to create an array that holds a collection of sounds that will play at random(only one sound per mouse down) when the left mouse key is pressed down. The thing is, I'm not very familiar with arrays so I'm not sure how to create a script that does this.
Could someone at least get me started because I have no idea how to start a script like this. Thanks in advance.
Comment
Answer by pankajb · May 14, 2016 at 01:40 AM
public List<AudioClip> myAudioClipsList = new List<AudioClip>();
void Start(){
// SoundFolder should be in Assets/Resources
myAudioClipsList.Add ((AudioClip)Resources.Load (" SoundFolder/youraudioClip1"));
myAudioClipsList.Add ((AudioClip)Resources.Load (" SoundFolder/youraudioClip2"));
void Update() {
if (Input.GetMouseButtonDown(0))
{
transform.GetComponent<AudioSource>().clip = myAudioClipsList[Random.Range(0,myAudioClipsList.Count)];
transform.GetComponent<AudioSource>().Play();
}
}