How do I play multiple audio clips from one audio source?

Hi, as the title states, I’m trying to get multiple audio clips to play from one audio source. Currently, I’m attempting to change the audio clip element of the audio source based on the value returned by “objectStatus.GetState()” which is an int between 1 and 3. I can only get one sound to play, and it’s not even playing at the right time. (The gallop sound plays while the horse is trotting) I do not have a audioclip for trot yet but I don’t think that’s a source of my problems. HALP PLEASE!!!

public class Horse_Sound_Mgr : MonoBehaviour
{
    private AudioSource a_source;
    private Follow_Script objectStatus;
   
    public AudioClip au_idle, au_gallop/*, au_trot*/;


    void Start()
    {
        objectStatus = GetComponent<Follow_Script>();       
        a_source = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        switch (objectStatus.GetState())
        {
            case 1: //Idle               
                a_source.audio.clip = au_idle;
                audio.Play();
                break;

            case 2: //Trot
                //a_source.audio.clip = au_trot;
                //audio.Play();
                break;

            case 3: //Gallop               
                a_source.audio.clip = au_gallop;
                audio.Play();
                break;
        }
    }
}

You’re not calling Audio.Stop() anywhere. Also, make sure Play on Awake is not checked and that the gallop sound is not set as the clip for the Audio Source in the Inspector. You might find some of the resources from Stop audiosource clip from playing - Questions & Answers - Unity Discussions helpful as well.

You are trying to play the audio every single frame, so they won’t play properly, because you’re always restarting them. The only time it will work is when you change to trot, then the clip from the state that you were on before will be allowed to play through because you aren’t resetting the audio when you trot.

Also maybe it is a detail but doing a_source.audio.clip is weird as a_source is already an AudioSource type you can just write a_source.clip and a_source.Play().
If there is an audioSource on the object you attached this script to, then doing audio.clip = … and audio.Play() also works fine.
You should not mix those two syntaxes it is kind of confusing :slight_smile:

Thanks for the pointers, but how do I play the clips without resetting them every frame?

Thanks for the tips Elvis, I will check out that link :slight_smile:

private Int PreviousFramesState = 0;
void Update()
{
    If (objectStatus.GetState() != PreviousFramesState)
    {
         PreviousFramesState = objectStatus.GetState();
         //Switch
    }
}

So it only runs the switch on the frames where the state has been changed.

Funny, I just came up with something similar and combined it with some other stuff I pulled off this video:
https://www.youtube.com/watch?v=-JFW1cbuJww

void Update()
    {
        soundID = objectStatus.GetState(); //update soundID to currentSound
       
        //GallopSound();
        if (soundTimer > 0)
        {
            soundTimer -= Time.deltaTime;
        }
        if (soundTimer < 0)
        {
            soundTimer = 0;
        }
        switch (objectStatus.GetState())
        {
            case 1: //Idle
                if (soundID != lastSoundID)     //compare current sound to sound from previous frame
                {
                    audio.Stop();
                    soundTimer = 0;
                    Debug.Log("Sound Change");
                }
                IdleSound();
                break;

            case 2: //Trot
                if(soundID != lastSoundID)
                {
                    audio.Stop();
                    soundTimer = 0;
                    Debug.Log("Sound Change");
                }
                break;

            case 3: //Gallop
                if (soundID != lastSoundID)
                {
                    audio.Stop();
                    soundTimer = 0;
                    Debug.Log("Sound Change");
                }
                GallopSound();
                break;
        }
        lastSoundID = objectStatus.GetState();
    }

    void IdleSound()
    {
        if (soundTimer <= 0)
        {
            audio.PlayOneShot(au_idle);
            soundTimer = au_idle.length;
        }
    }
    void GallopSound()
    {
        if (soundTimer <= 0)
        {
            audio.PlayOneShot(au_gallop);
            soundTimer = au_gallop.length;
        }
    }

Alls I need now is a trot audioclip! Thanks again for the help…