DontDestroyOnLoad() how?

Hi, I’ve got a coroutine that activates a game object after 4 minutes and there’s a sound that tells the player that this object is now available in the main level.

From the main level you can activate several mini games and I’d like the object that activates in the main level to activate after 4 minutes regardless of what level the player decides to load. I’ve read about DontDestroyOnLoad() but how or where do I implement this? should it be in the same script as the coroutine script for the object in question? this is the coroutine script:

using UnityEngine;
using System.Collections;

public class DelayedActivate2 : MonoBehaviour {

    public AudioClip Ring;
   
    void Start() {
        StartCoroutine(ActivateRoutine());
    }
   
    private IEnumerator ActivateRoutine() {
        
       
        yield return new WaitForSeconds(4.0f * 60.0f);    // and now we wait !
       
        // make a list of all children
        Transform[] ChildrenTransforms = this.gameObject.GetComponentsInChildren<Transform>(true);
        foreach( Transform t in ChildrenTransforms)
            t.gameObject.SetActive (true);      // enable all the objects in the list (even the parent)
            if (!audio.isPlaying) {
                        audio.clip = Ring;
                        audio.Play ();
                }
       
    }
}

I’m not sure I follow or that you even need DontDestroyOnLoad. Are there any GameObjects that are being destroyed on changing scenes that you would like to keep around? That’s the only reason to use DontDestroryOnLoad.

The only thing you need to not destroy is a variable telling when the count started. A game manager with this variable is all you need to stop from being destroyed, then every scene just need to check if it’s been the set amount of time since that.

1 Like

Try putting this script in DelayedActivate2 :

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

This will make the gameobject and hence it’s coroutine to persist even in other scenes.

1 Like

@shaderpop Well the issue was that the timer stopped when I activated another level. A solution to that problem would be to not destroy the timer when loading another level.

Thanks gamer_boy81 :smile: that was efficient and easy, it worked like a dream!! The timer on the object continues while loading other levels and thats exactly what I needed. :smile:

That could, of course, cause all the other variables to persist as well. It would be better practice to create a game manager as a singleton that is not destroyed. Or even use playerprefs if its just the one variable you wish to save.

Glad it helped GFFG :)…but I also agree with vintar here. If you have a lot of other stuff
also in this gameobject, then either move those stuff elsewhere or
abstract it out to a Manager.

Okay, I’ll look into it. I’m not so familiar with singletons yet as I’m still learning but I agree that it would be cleaner to have a game manager. For now though there are no issues but I’ll probably need it later in the game. Thanks guys

lazy singleton example :

using UnityEngine

public class GameManager : MonoBehaviour
{
     public static GameManager current
     public float timer;

     void Awake()
     {
           DontDestroyOnLoad(gameObject);
           if(current == null)
           {
                 current = this;
            }
      }
}

now you can call GameManger.current.timer from any script

1 Like