Persisting Start/Options music

Hey I was wondering if there is a way to have the same clip play nonstop throughout the options and start menu. I’d like the same song to play without being restarted. If someone could help out thatd be awesome!

public AudioClip[] levelMusicChangeArray;

private AudioSource audioSource;

void Awake()
{
    DontDestroyOnLoad(gameObject);
    Debug.Log("Don't destroy on load" + name);
    audioSource = GetComponent<AudioSource>();
}
void OnLevelWasLoaded(int level)
{
    AudioClip nowPlaying = levelMusicChangeArray[level];
    Debug.Log("Playing clip: " + levelMusicChangeArray[level]);
    // If there is some music attached.
    if (nowPlaying)
    {
        audioSource.clip = nowPlaying;
        audioSource.loop = true;
        audioSource.Play();
    }
}

It’s very simple, write the IF condition this way:

and leave the levelMusicChangeArray[2] empty (or put the same clip present in [1]) in the Inspector.

3 Likes

Hey thanks a lot, that’s a really simple way to do it lol. I was trying to use another script and I couldn’t get it to work right. Youre a legend mate!

Privacy & Terms