How to play separate audio files in sequence on collision?

Okay, not sure if this is the right place to ask or if I did something wrong but ?? aah
So, I have a trigger that I want to play an audio file whenever it is collided with, but I want it to play a different sound each time it is triggered. Not at random, either; I have a sequence of audio files that it must go in. And, once the sequence is completed, I want it to go back to the start. It should be able to repeat infinitely.
What I wrote, I can tell just by looking at it, is definitely not efficient or effective. But with my sparse knowledge of coding, it’s the only way I know how to do it, and it should be working. Plus, the game is not big or complicated, so I don’t have tons and tons of code to manage.
I haven’t seen anyone with the same issue online, so I’ve just had to fend for myself- I have no idea what the proper way to do this is.
My problem at the moment is that it will play the first audio file, but not move to the next one.
My silly way of writing it is 100% the problem, but that’s why I’m asking for help C:
Here’s my code, attached to the player character (which should be the thing to trigger the sound)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SOUNDScript : MonoBehaviour
{
    public bool collisionHasHappenedOne = false;
    public bool collisionHasHappenedTwo = false;
    public bool collisionHasHappenedThree = false;
    public bool collisionHasHappenedFour = false;
    public bool collisionHasHappenedFive = false;
    public bool collisionHasHappenedSix = false;
    public bool collisionHasHappenedSeven = false;
    public bool collisionHasHappenedEight = false;
    public AudioClip firstAudioClip;
    public AudioClip secondAudioClip;
    public AudioClip thirdAudioClip;
    public AudioClip fourthAudioClip;
    public AudioClip fifthAudioClip;
    public AudioClip sixthAudioClip;
    public AudioClip seventhAudioClip;
    public AudioClip eighthAudioClip;
    AudioSource source;

    void Start()
    {
        source = GetComponent<AudioSource>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collisionHasHappenedOne == false)
        {
            source.PlayOneShot (firstAudioClip);
            collisionHasHappenedOne = true;
        }
        if (collisionHasHappenedOne == true)
        {
            source.PlayOneShot (secondAudioClip);
            collisionHasHappenedTwo = true;
        }
        if (collisionHasHappenedTwo == true)
        {
            source.PlayOneShot(thirdAudioClip);
           collisionHasHappenedThree = true;
        }
        if (collisionHasHappenedThree == true)
        {
            source.PlayOneShot(fourthAudioClip);
            collisionHasHappenedFour = true;
        }
        if (collisionHasHappenedFour == true)
        {
            source.PlayOneShot(fifthAudioClip);
            collisionHasHappenedFive = true;
        }
        if (collisionHasHappenedFive == true)
        {
            source.PlayOneShot(sixthAudioClip);
            collisionHasHappenedSix = true;
        }
        if (collisionHasHappenedFive == true)
        {
            source.PlayOneShot(seventhAudioClip);
            collisionHasHappenedSeven = true;
        }
        if (collisionHasHappenedSeven == true)
        {
            source.PlayOneShot(eighthAudioClip);
            collisionHasHappenedEight = true;
        }
        if (collisionHasHappenedEight == true)
        {
            collisionHasHappenedOne = false;
            collisionHasHappenedTwo = false;
            collisionHasHappenedThree = false;
            collisionHasHappenedFour = false;
            collisionHasHappenedFive = false;
            collisionHasHappenedSix = false;
            collisionHasHappenedSeven = false;
            collisionHasHappenedEight = false;

        }
            }

}

Again, I am very much so aware that this is a silly way to do it, but I don’t know any other way. I don’t have any compiler or runtime errors, it just won’t play the next audio clip in the sequence.
I mean, I’m surprised anything is happening at all since I thought multiple if statements in a method wasn’t even valid?? anyway, any help would be appreciated, thank you :]

You definitely need to learn how to use collections: C# Arrays

You can have an array of the audio clips, and maintain an integer index value. On a collision, you can play the sound at the index, then increment said value, resetting it when you hit the end of the collection.

Ooh, okay! :thinking:
How would you do this ? I’ve been poking around and writing for a little bit and I don’t know how you would make an array of audio clips? Idk, arrays are pretty confusing :sob:

I fixed it!! For anyone with the same problem looking for a solution (since wheewf I know what it’s like scouring forums for hours to find someone with the same issue), this was what I did:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SOUNDScript : MonoBehaviour
{
    public AudioClip[] audioClips;
    private AudioSource source;
    private int currentClipIndex = 0;

    void Start()
    {
        source = GetComponent<AudioSource>();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (audioClips.Length == 0)
        {
            return;
        }
        source.PlayOneShot (audioClips[currentClipIndex]);

        currentClipIndex++;

        if (currentClipIndex >= audioClips.Length)
        {
            currentClipIndex = 0;
        }
            
    }
}

Remember (since this tripped me up at first) to set the audio files in the inspector for all the items in the array!

2 Likes

nice! From unity 23.2 we also have the Audio Random Container. It can do what you have already, but also random pitch/volume, shuffle/random modes, with clip overlaps + delays :slight_smile: