- Home /
Remnant Empty Game Objects for Static Scripts
One of my current projects is using a static, singleton class "System Manager" to handle control and data flow between several different classes in my game. It is attached to an empty Game Object. Everything works fine, EXCEPT between scene transitions, I end up with several blank game objects. It doesn't affect anything functionally, but it just seems like there would be a more "correct" way of doing what I have in mind that would prevent such a thing from happening. The only workaround I have is to have a function that constantly looks for empty Game Objects called System Manager and destroys them.
Answer by Munchy2007 · Dec 22, 2015 at 10:08 AM
You should check for duplicate instances in Awake() and remove them if found. This will ensure you only have one instance in the scene.
public static SystemManager instance;
void Awake()
{
if (instance != null) {
DestroyImmediate(gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
}
This worked. Funny enough, it was just a small, but obvious logic error I was missing that was the difference between what you had and my own code.
Your answer
Follow this Question
Related Questions
Destroy by Contact (OnTriggerEntry) - destroying more then i want. 0 Answers
DisplayText info and wait for animation ends to destroyobject ( time extra box) 0 Answers
Help with a simple procedural generation 0 Answers
Using static properties that return _instance.variable... 0 Answers
Bug caused by Singleton Pattern 1 Answer