How to change Music between Scenes
I've been trying to change scenes and have the music change with the different scenes while having a singleton in my core game area. I have made a singleton but that works as music for the whole game and does not separate the music from the title to the core game area.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicPlayer : MonoBehaviour
{
// My singleton
void Start()
{
if (FindObjectsOfType<MusicPlayer>().Length > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
}
I have tried using methods to change the clip in the audio source but only can get the method to work for on trigger enter and when used in another class/script it references the other classes audio source than my music player.
//The meathod to change music
public void ChangeBGM(AudioClip newDisk)
{
if (discDrive.clip.name == newDisk.name)
{
return;
}
else
{
discDrive.Stop();
discDrive.clip = newDisk;
discDrive.Play();
}
}
I also used these youtube video and still had the same problem: https://www.youtube.com/watch?v=XoH8Qyqje1g https://www.youtube.com/watch?v=uNX5NrHvfWY
The closest I got to my goal was just using a different audio source and clip for each scene but that doesn't allow my singleton to work in the core game area or the ability to go back to the main menu to hear its music.
Any help would be appreciated.