- Home /
Removing duplicates with DontDestroyOnLoad
I am trying to get rid of duplicates created when a scene containing GameObjects using DontDestroyOnLoad is reloaded. I want to control the DontDestroyOnLoad behaviour of several objects within a master node.
I tried using the solution proposed in this topic: http://answers.unity3d.com/questions/34185/dontdestroyonload-is-it-intended-behavior.html, but it doesn't work from my master node. It works from within the GameObjects themselves if I copy-paste the code into each of them, but this is not how I want to handle the destruction behaviour. My master node contains the following code:
public class StateSaver : MonoBehaviour {
private static bool created = false;
void Awake(){
if(!created){
DontDestroyOnLoad(this.gameObject);
DontDestroyOnLoad(GameObject.Find("FSM"));
DontDestroyOnLoad(GameObject.Find("OSCObject"));
DontDestroyOnLoad(GameObject.Find("GameModeDisplay"));
DontDestroyOnLoad(GameObject.Find("Pauser"));
created = true;
}
else{
Destroy(this.gameObject);
Destroy(GameObject.Find("FSM"));
Destroy(GameObject.Find("OSCObject"));
Destroy(GameObject.Find("GameModeDisplay"));
Destroy(GameObject.Find("Pauser"));
}
}
}
I know that Find is not very efficient, but it is not a concern at this point. I assume that my master node fails because GameObject.Find("X") might not return the object I want. Is there a way of achieving this?
Your answer
