GameControl always null?
Hi, I don't know what is wrong with my destroy portion of the below code. I'm trying to have only 1 GameControl persist throughout the scenes. It seems that every time I switch back to this scene, my GameControl is read as null and a new GameControl is generated and I end up having more than 1 GameControl persisting.
For reference: I tried to port the code from 18:24 to JavaScript from this https://www.youtube.com/watch?v=yxziv4ISfys
Please advise.
#pragma strict
var control : GameControl;
function Awake () {
Debug.Log("GameControl runs");
if (control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
Debug.Log(control);
} else if (control != this)
{
Destroy(gameObject);
Debug.Log("Destroy?");
}
}
function Update () {
}
Answer by wsgb · Nov 10, 2015 at 10:08 AM
I had to initiate the GameControl as "static var control : GameControl;" so that the value can persist throughout all scenes rather than recreate itself each time with the referenced value in the script.
Your answer
![](https://koobas.hobune.stream/wayback/20220612044547im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to delete tiles when ran over to avoid lag? 0 Answers
How to make it so when i shoot a zombie the has been spawned, it will destroy it? 1 Answer
Why i can't acces a global list in my class from OnDestroy() function 0 Answers
Why aren't my prefab clones destroying on play mode exit? 1 Answer
Is there a way to Destroy a GameObject with given Coordinates? 2 Answers