Unity 3d singleton problem

Hi

I am having a problem with Singleton that I have created as

public static GameManager Instance()
{
        if (_sharedManager == null)
        {
			Object []objects = FindObjectsOfType(typeof(GameManager));
			if(objects == null || objects.Length == 0)
			{
				GameObject singleton = new GameObject("singleton");
				_sharedManager = singleton.AddComponent<GameManager>();
				_sharedManager.Initialize();
				
				DontDestroyOnLoad(singleton);
			}
			else if(objects.Length == 1)
			{
				_sharedManager = (GameManager)objects[0];
				DontDestroyOnLoad(_sharedManager.gameObject);
			}
			else 
			{
				throw new System.Exception("too many GameManagers");
			}
        }

        return _sharedManager; 
}

this works perfectly well as long I am in the same scene through which first time GameManager.Instance() called, but when I switch my scene or load another level then sharedManager is still not null but two of its data members which or of type Unity.Object has value equal to null which was valid object before scene switch

Please help me what I am doing wrong

Ahsan

There is also a very good singleton reference on UA, located here. It also has a few alternatives to the singleton pattern.

Are the two objects that this singleton references also marked with DontDestroyOnLoad? I have a strong feeling they aren’t; marking the singleton with DontDestroyOnLoad doesn’t keep any of its references alive during a scene change. So what happens is your referenced objects get Garbage Collected during a scene change.