I have 2 buttons in my options menu, "return to main menu" and "mute music" Each of these buttons has a script attached to it, and calls the OnPress() method of the script when it is pressed. I also have a main Level object/script that handles all the scene loading and stuff. So the script of the main menu button does FindObjectOfType() in its Start and then calls level.LoadStartScene() in its OnPress(). The script for the mute button does the same thing but calls level.ToggleMuteMusic(). So this worked perfectly before, but then I made the level a singleton with the following code:
public void Awake() {
InitializeSingleton();
}
private void InitializeSingleton() {
if (FindObjectsOfType(GetType()).Length > 1) {
Destroy(gameObject);
} else {
DontDestroyOnLoad(gameObject);
}
}
So now the main menu button works perfectly, but the mute button gives an error; I think this is because in Start() it finds the old level object, and then the one with DontDestroyOnLoad comes in and deletes the old one, but then why does the main menu button work???
Mute Button Code:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class MuteButton : MonoBehaviour {
[SerializeField] string mutedText = "Unmute Music";
[SerializeField] string unmutedText = "Mute Music";
private Level level;
private TextMeshProUGUI myText;
public void Start() {
level = FindObjectOfType<Level>();
myText = GetComponent<TextMeshProUGUI>();
}
public void OnPress() {
if (level == null) {
Debug.Log("log 1");
}
level.ToggleMuteMusic();
bool muteMusic = level.GetMuteMusic();
if (muteMusic == true) {
myText.SetText(mutedText);
} else if (muteMusic == false) {
myText.SetText(unmutedText);
}
}
}
Menu Button Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuButton : MonoBehaviour {
Level level;
public void Start() {
level = FindObjectOfType<Level>();
}
public void OnPress() {
level.LoadStartScene();
}
}
Full Error:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.GameObject.GetComponent[T] () (at C:/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28) Level.ToggleMuteMusic () (at Assets/Scripts/Level.cs:74) MuteButton.OnPress () (at Assets/Scripts/MuteButton.cs:23)
Thanks for your time :)