- Home /
RogueLike tutorial GameManager question
On line 25 in the GameManager script, the singleton is enforced by calling Destroy(gameObject)
when instance != this
My question is, won't the code after Destroy
(lines 31-37) still get executed on that condition and be a waste? Initially I thought Destroy
might break execution, but I added a Debug.Log
after it which got executed. Am I missing something?
Answer by thor348k · Aug 27, 2016 at 06:39 AM
I hope this helps, I added some comments. Good luck :)
void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
/*
Notice how it's the only line encapsulated?
This means it's the only thing that runs.
Curly braces make code more readable :)
Side Note:
Once an object is destroyed, it is removed from the scene.
It's like breaking from a loop, or returning from a function.
All logic stops. In this case, it no longer exists.
*/
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
boardScript = GetComponent<BoardManager>();
InitGame();
}
Thanks for your help. I assumed that was the case, but while experimenting I noticed that Destroy does not appear to stop all logic.
void Awake () {
Destroy (gameObject);
// This log still occurs
Debug.Log ("After Destroy (gameObject)");
}
Any idea why the log still occurs?
Destroy() doesn't destroy an object immediately. It's just marked as "to destroy" and will be destroyed at the end of the frame.
I never knew that. I just assumed it always was destroyed immediately because I never have any logic following it. Thanks!
Your answer
