Java Script Dont Destroy On Load Duplicates Game Object

I have this don’t destroy on load java script in order to make a timer keep time across scene changes, however when I return to my scene my timer game object has been duplicated!!
Is there a way to fix that?

Sounds like you have something in your scene that is Instantiating the timer. Do you have a game/scene manager object that you use to put the timer into a scene? If you do just remove it from all the scenes apart from the first one (or at least just remove the bit that tells the timer to appear)

You will only need it in the first scene as the same timer will be there due to the DontDestroyOnLoad command.


you can do it by checking which level you want to implement the dontdestryonload eg:

void Start()
   if(Application.loadedLevelName != "Your Level Name which you dont want to duplicate");
   DontDestroyOnLoad ("your item");

Its in C# change it to Jscript

On Unity Forums I found this piece of code that cured my duplication of my “CountdownTimerManager” game object:

But I still am having issues where my timer will no longer work when I go back into my main scene because the UI text is no longer connected to the script, you will have to see my screen-shot image attached in my post above to see how my Hierarchy is set up. I’m not even sure if this is even blasted well possible with the new UI system???

function Awake ()
     // Make LevelData a singleton, so we keep ourself alive and kill any clones
     if ( FindObjectsOfType(CountdownTimerManager).Length != 1 )     // If one of me already exists I must be a clone, so destroy me (don't feel bad about it)
         Destroy (gameObject);
     else        // I'm the original, so keep me around
         DontDestroyOnLoad (gameObject);