DontDestroyOnLoad Problem

I am programming a game entirely out of UI (for now). It consists of various scenes which are menus. For example, the Main Menu is one scene, and there is button that brings you to another scene (menu). On the main menu, there is a text object which increments by 1 every second. When you go to the next menu, this continues to increment (but the text object is disabled and re-enabled when you go back). The problem is, it seems that another text object is created when I go back, and overlaps the previous. So for example, lets say when I got back, the object incremented to 50, there will be a new text on top of that one that is starting from 0 again… Here is my code:

	void Awake(){
		DontDestroyOnLoad (transform.root.gameObject);	
	}

	void Start () 
	{
		AutoGold ();
	}

	void Update(){
		if (Application.loadedLevelName != "MainMenu") 
		{
			GetComponent<Text> ().enabled = false;
		} else 
		{
			GetComponent<Text> ().enabled = true;
		}
	}

There is more code, but this is more than likely where my problem is happening…any help?

follow this: http://wiki.unity3d.com/index.php/Singleton

so you code should like:

`

 public static YourClassName someName;

 void Awake(){

  DontDestroyOnLoad (transform.root.gameObject);    
    if (someName == null)
  {
        someName = this;
    }
   else
    {
           Destroy(transform.root.gameObject);
    }
}

 void Start () 
 {
     AutoGold ();
 }

 void Update(){
     if (Application.loadedLevelName != "MainMenu") 
     {
         GetComponent<Text> ().enabled = false;
     } else 
     {
         GetComponent<Text> ().enabled = true;
     }
 }

`

That did solve the overlapping problem, so thanks! But I do have another issue now. I have 10 text boxes which all use this particular script. There are variables which set each text box to its proper text, stat, etc. For some reason when I use the code that you provided, it is leaving only one text box (strangely, not the first but the second in the list?) and is deleting the rest. I used transform.gameObject by the way because when I used root, it deleted everything from the scene entirely. Any idea? Here is my code. Basically I assign to each text box the oreType, and the script increments the amounts according to the time provided in InvokeRepeat. This portion works as expected. (sorry the code is so long)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class OreManager : MonoBehaviour {

	public string oreType;
	public bool oreUnlocked;
	private float[] oreAmount = {0,0,0,0,0,0,0,0,0,0};
	private float[] orePerSecond = {1.0f,0.5f,0.5f,0.25f,0.25f,0.125f,0.0625f,0.03125f,0.015625f,0.0078125f};
	public static OreManager instance;

	void Awake(){
		DontDestroyOnLoad (transform.gameObject);
		if (instance == null) 
		{
			instance = this;

		} else 
		{
			Destroy (transform.gameObject);
		}
	}
	void Start()
	{
		AutoOre ();
	}

	void Update()
	{
		if (Application.loadedLevelName != "MiningMenu") 
		{
			GetComponent<Text> ().enabled = false;
		} else 
		{
			GetComponent<Text> ().enabled = true;
		}
	}

	public void AutoOre()
	{
		Text display = GetComponent<Text> ();
		switch (oreType) 
		{
		case "Stone" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[0]),(1/orePerSecond[0]));
			}
			else
			{
				display.text = "LOCKED";
			}
				break;
		case "Copper" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[1]),(1/orePerSecond[1]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Tin" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[2]),(1/orePerSecond[2]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Iron" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[3]),(1/orePerSecond[3]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Coal" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[4]),(1/orePerSecond[4]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Silver" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[5]),(1/orePerSecond[5]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Gold" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[6]),(1/orePerSecond[6]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Titanium" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[7]),(1/orePerSecond[7]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Diamond" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[8]),(1/orePerSecond[8]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		case "Dragonstone" :
			if(oreUnlocked)
			{
				InvokeRepeating ("DisplayOre",(1/orePerSecond[9]),(1/orePerSecond[9]));
			}
			else
			{
				display.text = "LOCKED";
			}
			break;
		}
	}

	public void DisplayOre()
	{
		Text display = GetComponent<Text> ();
		switch (oreType) 
		{
		case "Stone" :
			oreAmount[0] += 1.0f;
			display.text = oreType + "

" + oreAmount[0];
break;
case “Copper” :
oreAmount[1] += 1.0f;
display.text = oreType + "
" + oreAmount[1];
break;
case “Tin” :
oreAmount[2] += 1;
display.text = oreType + "
" + oreAmount[2];
break;
case “Iron” :
oreAmount[3] += 1;
display.text = oreType + "
" + oreAmount[3];
break;
case “Coal” :
oreAmount[4] += 1;
display.text = oreType + "
" + oreAmount[4];
break;
case “Silver” :
oreAmount[5] += 1;
display.text = oreType + "
" + oreAmount[5];
break;
case “Gold” :
oreAmount[6] += 1;
display.text = oreType + "
" + oreAmount[6];
break;
case “Titanium” :
oreAmount[7] += 1;
display.text = oreType + "
" + oreAmount[7];
break;
case “Diamond” :
oreAmount[8] += 1;
display.text = oreType + "
" + oreAmount[8];
break;
case “Dragonstone” :
oreAmount[9] += 1;
display.text = oreType + "
" + oreAmount[9];
break;
}
}

	/*public void UpgradeOrePerSecond(float inc)
	{
		CancelInvoke ("DisplayOre");
		orePerSecond += inc;
	}*/
}