LoadLevel and preventing some new objection creation

I’ve never programmed in Unity (or any 3D engine) before a month ago when I got a massive project dropped in my lap with no other Unity developers on staff and not the guys who originally wrote it.

I’m trying to implement LoadLevel which it is clear the design was never meant to support but I need to add it anyways.

Before I call LoadLevel I call DontDestroyOnLoad for a few key objects but I was surprised to find the when I call LoadLevel, new instances of those objects are created. So now I got doubles.

Since whole scenes reload, how do I have a single class manage what is going on overall in the game?

I can only think of using a static class as a game manager.

Is that the solution or am I missing a broader concept in working in Unity?

Try something like this:

public class MyClass : MonoBehaviour
{
 static MyClass instance;
 public static MyClass Instance
 {
  get
  {
   if(instance == null)
   {
    instance = new GameObject("MyClass").AddComponent<MyClass>();
    DontDestroyOnLoad(instance.gameObject);
   }
   return instance;
  }
 }

 void Awake()
 {
  if(instance != null)
  {
   Destroy(gameObject);
  }
 }
}

This will cause the second creation of any particular class to be instantly deleted. It will also mean if you try to access it without having one created it will automatically create the first one. So on the second time a level is loaded it’s going to use the old versions of those classes and destroy the new ones.