- Home /
Best Way to Sequence AudioClips in an Array
Hey everyone. I'm new to C# scripting in Unity and am trying to add audio scripting to the Unity "Roll-A-Ball" tutorial. I have set up an array with different audio clips. I want to sequence these clips such that every time the player picks up one of the pickups, an audio clip will play. Picking up the next one would play the next clip in the sequence, and so on and so forth. I was able to achieve this with many "if" statements:
void OnTriggerEnter(Collider other)
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
if (count == 1)
{
puaudio.PlayOneShot (sounds[1]);
}
if (count == 2)
{
puaudio.PlayOneShot (sounds[2]);
}
if (count == 3)
{
And so on, all the way up to if (count == 12).
My question is, is there a better way to sequence the AudioClips in an array for this effect, or do I have to create an "if" statement for each one in the sequence? Thanks!
Answer by Positive7 · Aug 21, 2015 at 11:30 AM
int count = 1;
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
puadio.PlayOneShot (sounds[count]);
count++;
}
}
That works great! I thought there would be a more elegant way to do it, but I was unfamiliar with ++. Thank you so much!
Your answer
Follow this Question
Related Questions
AudioClip dont listened in script component 1 Answer
Play Audio in sequence from children objects 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Does C# Support Array of Objects? 1 Answer