- Home /
Execution order of Destroy() and DontDestroyOnLoad() between Scenes
Hello people,
I am writing here because i did not find anything helpful for my question.
I am working with GameManager, its static Instance and its use while changing scenes.
I have this scenario: 3 scenes ("GameMenu", "SPGame", "MPGame") and each one has a GameManager instance. Then I have written this code:
1) First Unity Methods:
private void Awake()
{
PrintExecutionLocation(this);
SetUpGameManager();
}
private void OnEnable()
{
PrintExecutionLocation(this);
SceneManager.sceneLoaded += OnSceneLoaded;
}
OnSceneLoaded():
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
PrintExecutionLocation(this);
Debug.Log(scene.name + " loaded with " + mode.ToString() + " mode");
Debug.Log("Looking for necessary parameters for the current scene...");
GetSceneComponents(scene.name);
}
2) Custom Methods
SetupGameManager():
private void SetUpGameManager()
{
PrintExecutionLocation(this);
if (CheckInstance() is true)
{
CheckMode();
}
GetSceneComponents(SceneManager.GetActiveScene().name);
}
CheckInstance():
private bool CheckInstance()
{
PrintExecutionLocation(this);
// if there is another istance of GameManager
if (instance != null && instance != this)
{
Debug.LogWarning("There is already a GameManager instance, i destroy myself (ID: " + GetHashCode() +").");
// destroy it
Destroy(gameObject);
return false;
}
else
{
Debug.LogWarning("There is not a GameManager, i will not destroy myself when loading new scenes (ID: " + GetHashCode() + ").");
instance = this;
// don't destroy the game manager (mainly to keep the score) while changing the levels (aka scenes)
DontDestroyOnLoad(gameObject);
return true;
}
}
CheckMode():
private void CheckMode()
{
PrintExecutionLocation(this);
//If i'm not in main menu scene, activate development mode
Debug.Log("Defining the current scene.");
switch (SceneManager.GetActiveScene().buildIndex)
{
case 0:
//Debug.Log("Sto cominciando dal menù principale.");
_developerMode = false;
Debug.Log("Developer Mode: OFF");
break;
case 1:
//Debug.Log("Sto cominciando dal Single player.");
_developerMode = true;
Debug.Log("Developer Mode: ON");
isSPG = true;
break;
case 2:
//Debug.Log("Sto cominciando dal Multiplayer.");
_developerMode = true;
Debug.Log("Developer Mode: ON");
isSPG = false;
break;
default:
Debug.LogError(SceneManager.GetActiveScene().buildIndex.ToString() + " is not recognized as Scene Index.");
break;
}
}
GetSceneComponents():
private void GetSceneComponents(string index)
{
PrintExecutionLocation(this);
switch (index)
{
case "GameMenu":
Debug.Log("Looking for Canvas references of main menu, settings menu, highscores menu...");
mainMenu = GameObject.Find("MainMenuCanvas").GetComponent<Canvas>();
settings = GameObject.Find("SettingsCanvas").GetComponent<Canvas>();
highscore = GameObject.Find("HighscoreCanvas").GetComponent<Canvas>();
if (mainMenu && settings && highscore)
{
Debug.Log("All Canvas references found.");
}
else
{
Debug.LogError("I did not find all Canvas references.\nWrite more code to find what's missing.");
}
break;
case "SPGame":
Debug.Log("Taking Player reference...");
player1 = GameObject.Find("Paddle1").GetComponent<Paddle>();
if (player1)
{
Debug.Log("Player reference found.");
}
else
{
Debug.LogError("Player reference not found.");
}
break;
case "MPGame":
Debug.Log("Taking 1st Player reference...");
player1 = GameObject.Find("Paddle1").GetComponent<Paddle>();
if (player1 != null)
{
Debug.Log("1st Player reference found, taking 2nd Player reference...");
player2 = GameObject.Find("Paddle2").GetComponent<Paddle>();
if (player2 != null)
{
Debug.Log("2nd Player reference found.");
}
else
{
Debug.LogError("2nd Player reference not found.");
}
}
else
{
Debug.LogError("1st Player reference not found.");
}
break;
default:
Debug.LogError("Scene not correct.");
break;
}
highscoreSystem = GetComponent<HighscoreSystem>();
}
And i got this Log:
I am wondering why the object still exists after the Destroy() method.
Could you explain to me which is the execution order when using the Destroy() method?
What about DontDestroyOnLoad() of GameManager when loading new Scene?
Thanks in advance.
Your answer
Follow this Question
Related Questions
How to disable Dont destroy on load 0 Answers
Access loading screen scene components from Main Menu? 1 Answer
Custom 'wipe' Transition between scenes 1 Answer
Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer
How do I make a level-to-level loading screen with bar? 1 Answer