Objects being duplicated with DontDestroyOnLoad

I’ve got a class I call MCP (master control program) attached to a GameObject that I’m using to store things like game state and settings. It is part of the FrontEnd scene file hierarchy. Since it has to be shared throughout all of our levels I’m calling DontDestroyOnLoad() during MCP.Awake(). This is fine when leaving the front end and going into the various levels of our game because the MCP stays around. However, when I come back to the FrontEnd, there’s now two copies of it, which is causing all sorts of conflicts. Is there a way to have an object be kept around but not duplicated when returning to the scene that it defined in?
Thanks
-Mo

Singltone is too hard, prefab is too ugly?

public void Awake()
    {
        DontDestroyOnLoad(this);

        if (FindObjectsOfType(GetType()).Length > 1)
        {
            Destroy(gameObject);
        }
    }

Yes it is called the ‘singleton pattern’. You can find tons of answers on this subject by searching for ‘singleton’ in Answers, Forums or even Google.

Basically you will hold a static reference to this object. And create the GameObject only when the reference is null (first access).

You could use static variable of your script to check if there is an instance of your script present, if so, destroy duplicate component.

Lets say that you have empty game object called GameController, with attached class GameMachine.

It would look like this:

using UnityEngine;

    public class GameMachine: MonoBehaviour
    {
    	private static GameMachine instanceRef;
    
    	void Awake()
    	{
    		if(instanceRef == null)
    		{
    			instanceRef = this;
    			DontDestroyOnLoad(gameObject);
    		}else
    		{
    			DestroyImmediate(gameObject);
    		}
    	}
    
    	
    }

Have a script in FrontEnd level that instantiate the MCP GameObject if it doesn’t exist.

var mcpPrefab : GameObject;

function Awake () {
    if (!GameObject.Find("MCP")) {
        var mcpTmp : GameObject = Instantiate(mcpPrefab, Vector3.zero, Quaternion.identity);
        mcpTmp.name = "MCP";
    }
}

you prevent to get duplicate by checking the level

                if(Application.loadedLevelName != "Menu")
		DontDestroyOnLoad(this);

The common singleton method might work in general, but if you’re having some specific static instance functions through a child of that MCP game object, it won’t do any good.

This “issue” comes from the fact that your reloading the same scene which originally included the original “MCP” file.

The most easy and quick solution to fix the DontDestroyOnLoad for a “master” gameobject that is kept through the whole game until it’s closed is to create an initial scene with that gameobject and never return to that scene while the game runs. Usually, you could use a scene that act as a custom splashscreen with logos and copyrights small warning for such thing which then switch to the actual “scene” that act as the main menu or whatever. Then, the player won’t be able to return to the splashscreen part without closing and reopening the game or app.

=EDIT=
For the sake of those who are looking at this and who are using either the Multiplayer Lobby or Network Manager features in Unity, you should know that whenever the player is disconnected from the server (or his own server), the scene will return to the Lobby/Offline scene regardless of if it’s the same scene as it’s currently loaded. So, creating a kind of “switch” scene or splashscreen scene is essential for such situations.