- Home /
Gameobject DontDestroyonLoad references not working after changing scene
Hi, need help in referencing objects in the inspector.
I have 2 scenes I,e main_menu and level. At scene main_menu there are a couple of game objects that are dontdestroyonload. on those objects sound effects and game music and scripts are attached. When I select the level from the main menu it works fine till now I can play game without any error or issue which is scene 2 l(named level 3). when I go back to scene 1 that is the main menu references in the inspector to the objects under objects remove or null it says missing object but object is present under dontdestroyonload . As in image 2.
Answer by rh_galaxy · Nov 10, 2021 at 11:23 AM
I guess that this is why... It was like this for me at least, before I fixed it.
When you go back to another scene working the first time, the do-not-destroy object may/will be created again. And if you don't want that you must also make the objects Awake() function to "fix" this by destroy the new object (not the old one from before, or you will invalidate the references you hold).
This is how it can be done
public static GameManager theGM = null;
//first code to run
void Awake()
{
//singleton
if (theGM == null)
{
theGM = this;
}
else if (theGM != this)
{
//enforce singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject); //<- Be careful, this makes OnDestroy() be called and we don't
// want to deinit everything there (for example if you have external things setup the
// first time that you want to keep function)
return;
}
//the rest is done once only...
DontDestroyOnLoad(gameObject);
//...
}
//example:
//cannot do this, because it is called when Awake is called a 2nd time when loading another scene!
// so OnDestroy() gets called for the new object that is then destroyed to enforce singleton
//we only want to do this when the app exits
/*private void OnDestroy()
{
if (!bSteamAPIInited)
return;
SteamAPI.Shutdown();
}*/
If this doesn't help you, share the relevant code, so we can look at it.
In Awake funtion checking for instance null or not still not working
public class MusicSetting : MonoBehaviour { public AudioSource audioSrc; public AudioMixer audioMixer; public Toggle MusicToggle; static MusicSetting instance = null; private float musicVolume = 1f;
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
}
DontDestroyOnLoad(gameObject);
}
void Start()
{
// Assign Audio Source component to control it
audioSrc = GetComponent<AudioSource>();
CheckMuteOrUnMute();
}
void CheckMuteOrUnMute()
{
if (PlayerPrefs.GetInt("MusicMute", 0) == 0)
{
Debug.Log("Music Un Mute");
audioSrc.mute = false;
MusicToggle.isOn = false;
}
else
{
Debug.Log("Music Mute");
audioSrc.mute = true;
MusicToggle.isOn = true;
}
}
public void MuteMusic(bool muteMusic)// Update player prefrences
{
if (PlayerPrefs.GetInt("MasterMute", 0) == 0)
{
Debug.Log("Master music is unmute");
audioSrc.mute = muteMusic;
if (muteMusic)
{
PlayerPrefs.SetInt("MusicMute", 1);
}
else
{
PlayerPrefs.SetInt("MusicMute", 0); // Music is unmute
}
}
else
{
Debug.Log("Master music is mute");
Debug.Log("so music is mute");
PlayerPrefs.SetInt("MusicMute", 1);
}
}
public void VolumeofMusic(float volume)
{
audioSrc.volume = volume;
Debug.Log("audioS.name " + audioSrc.name);
Debug.Log("audioS.volume " + audioSrc.volume);
Debug.Log("volume " + volume);
}
}
You are missing the return in your instance check... Edit: try also to make it exactly as my first suggestion, I think there is a difference
void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
}
i tried but still the same problem. Other object references working fine but the issue is with the objects under DontDestroryOnLoad.