Overlapping sounds is not working

Hello,

I can’t seem to get my audioclips to play at the same time without cutting each other off. As near as I can tell, every time an audioclip is played it is played through the same audiosource. I have three audiosources available for my SoundManager script to use, and I have tried to use audiosource.isPlaying to alternate between them; however, I set up a debug test in Start() in my PlayerController script where the game starts by playing three sounds in a row. The logs show the same audiosource being used and, of course, I only hear the one sound - but the logs also show each sound being played (before being “cut off”).

I am using Unity 5.6, but I had this issue in Unity 5.5 before I upgraded this week. Most of my sounds are triggered by collision between the player object and other gameobjects in the scene. In ordinary game play, the appropriate sounds play correctly on collision, but if the player collides with another object before the old sound completes the new sound is played and the old sound is abruptly stopped. I have tried both Play() and PlayOneShot() without any change.

Some of the relevant code:
-snipped from my SoundManager script which is called from PlayerController (below)

public class SoundManager : MonoBehaviour {

    public AudioSource efxSource;
    public AudioSource musicSource;
    public AudioSource thirdSource;
    public static SoundManager instance = null;
    public float lowPitchRange = .95f;
    public float highPitchRange = 1.05f;

    // Use this for initialization
    void Awake ()
    {
        if (instance == null)
            instance = this;
        else if (instance != null) {
            Debug.Log ("destroying sound instance");
            //Destroy (gameObject);
        }

        DontDestroyOnLoad (gameObject);
    }

    public void PlaySingle (AudioClip clip)
    {
        efxSource.clip = clip;
        if (efxSource.isPlaying) {
            PlayMajor (clip);
        } else {
            efxSource.Play();
            Debug.Log ("***efxsource played");
        }
    }

    public void PlayMajor (AudioClip clip)
    {
        musicSource.clip = clip;
        if (musicSource.isPlaying) {
            PlayThird (clip);
        } else {
            musicSource.Play();
            Debug.Log ("***musicsource played");
        }
    }

    public void PlayThird (AudioClip clip)
    {
        thirdSource.PlayOneShot (clip);
        Debug.Log ("***thirdsource played");
    }

-snipped from PlayerController

void Start ()
    {
        ended = false;
        complete = false;
        gameManager = Loader.FindObjectOfType<GameManager>();
        soundManager = Loader.FindObjectOfType<SoundManager> ();
        level = gameManager.getLevel ();

        //test soundsources
        soundManager.PlaySingle(bonusSound);
        Debug.Log ("***sound 1 played");
        soundManager.PlaySingle (powerUpSound);
        Debug.Log ("***sound 2 played");
        soundManager.PlaySingle (pickUpSound);
        Debug.Log ("***sound 3 played");

And a record from my log in the editor:

It never seems to try SoundManager.PlayMajor().

You’re assigning the clip even though the AudioSource might be playing.

1 Like

You know, I had a similar thought about that when I woke up this morning and then I see your comment. And it seems to have made a big difference. It is definitely playing more than one sound now; however, after playing from efxsource, the next two sounds are played through thirdsource. I added a debug line to musicsource.isplaying and that shows up when the 2nd and 3rd sound is attempted. I don’t see anything in my PlayerController script or my GameManager and LevelGenerator scripts that are calling sounds at start up so I don’t know why the musicsource audiosource is busy. Is there a limit to pooling or something wrong with my code logic?

Well, thanks for the help. The sounds, two of them at least for certain, do seem to be playing over each other now.

Here’s the revised code for my SoundManager:

public void PlaySingle (AudioClip clip)
    {
      
        if (efxSource.isPlaying) {
            PlayMajor (clip);
        } else {
            efxSource.clip = clip;
            efxSource.Play();
            Debug.Log ("***efxsource played");
        }
    }

    public void PlayMajor (AudioClip clip)
    {
      
        if (musicSource.isPlaying) {
            Debug.Log ("***musicsource is playing");
            PlayThird (clip);
        } else {
            musicSource.clip = clip;
            musicSource.Play();
            Debug.Log ("***musicsource played");
        }
    }

    public void PlayThird (AudioClip clip)
    {
        thirdSource.PlayOneShot (clip);
        Debug.Log ("***thirdsource played");
    }