- Home /
Best workflow for DontDestroyOnLoad method regarding Unity Editor?
I am creating my first game in Unity, nothing big actually, but I have run into a situation, where I would like to hear some advices on best approach. Basically I have Scene 0, which is the main menu, and Scene 1, which is the game. In scene 0 I would like to read in a few values with PlayerPrefs and assign them in a gameobject called GameController. The reason for this is, that the user would be able to change these values in a UI, which is also in Scene 0. In scene 1 I am using these values too to manipulate the game itself. But the GameController gets destroyed on switching scenes, so I would need to read the values in again, which I am trying to avoid. The GameController is actually identical in both scenes, so I thought to use DontDestroyOnLoad, but then I would have to remove it from the scene 1 and that is where I see some issues.
I can not set references to it in other game object in the Inspector.
If I wanted to change something in the game controller I would have to swtich scene
I dont see the GameController in the Hierarchy in Scene 1, which I find confusing sometimes, because working with something, that is not there (or atleast not visible)?!?!
Any ideas how to handle these cases?
Or do you think it would be too big of a hastle and I should just let it reconstruct? Would it be a "normal" process that all games do? I am unexperienced with this.
Answer by rh_galaxy · Apr 23, 2020 at 06:03 PM
I have a GameManager as Do not destroy (DND) and singleton. Then have the GameManager transfer everything needed from the menu to the gamelevel scene objects through internal static variables in the transition.
public class GameManager : MonoBehaviour
{
public static GameManager theGM = null;
//first code to run
void Awake()
{
//singleton
if (theGM == null)
{
theGM = this;
}
else if (theGM != this)
{
//enforce singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject); //<- this makes OnDestroy() be called and we don't want to deinit everything there
return;
}
//the rest is done once only...
DontDestroyOnLoad(gameObject);
//...
}
}
I also have the camera class as DND to avoid having two or no camera at the scene transition time, and set the camera class from the GameManager for either menu mode or game mode.
But this is just how I do it, I'm not at all sure it's the best thing, but it works.
This would need to be the first component after the transform component, right? so it would be constructed before all the other scripts (which do the reading with playerprefs) and destroyed if already loaded?
Edit: I just tryed, if you assign the gamecontroller from the game scene to a component of another gameobject in inspector as reference, it is lost once the game starts. Actually that way you assign the gamecontroller from the game scene, but once the game runs it is not there, there is the gamecontroller from the menu scene ins$$anonymous$$d, which is a different gameobject. This means that your answer is not what I am searching for.
You will have to attach the Game$$anonymous$$anager script to an empty gameobject created in the first scene you run.