- Home /
How to use DontDestroyOnLoad() only with certain scenes
Hello everyone, recently I substituted my PlayerPrefs method of saving data to a custom Save/Load system. However, the SaveData() method requires references to other scripts in the main menu, which is totally fine. Though in the level itself, if I want to save the data, I wouldn't have the scripts to reference (because they reside in the main menu). If I use DontDestroyOnLoad(), I've noticed that the original references to objects in the main menu (where the scripts are originally from) get undone. So if I loaded the level, and went back to the menu, all the public variables would be unconnected. How do I take the objects to the next scene, but if returned to the MainMenu, use the objects that have the references? Thanks for any help.
Answer by Diogo_NullMoon · Sep 06, 2019 at 11:16 PM
I create two GameObejcts, one just have a simple instantiete code...`
GameObject gameController;
public GameObject gameControllerPrefab;
public Transform gameControllerPrefabTransform;
void Awake() {
// Search for a GameController
gameController = GameObject.FindWithTag("GameController");
if (gameController == null){ // if dont have one, create it
gameController = gameControllerPrefab;
Instantiate(gameController, gameControllerPrefabTransform.position, gameControllerPrefabTransform.rotation);
}
}`
and the second have the "DontDestroyOnLoad()"
$$anonymous$$aking prefabs of my required scripts to then instantiate them on my in-game scene sounds like a great idea! Thank you so much, trying this tomorrow.
Instantiating the objects in the in-level scene to then reference then and use my SaveData() method did the trick! Thanks.
Answer by surfuay · Sep 06, 2019 at 02:13 AM
whatever script is your Don'tDestroyOnLoad needs to contain a reference to any and all variables that you want to persist
IE score
Main Menu has a high score display/ don't destroy has a saved score/ level has a current score
so it would look something like this
Main Menu (this will update your high score every time your Menu Scene Loads in and only then)
public int HighScore = 0; (can also be written public int HighScore; or what ever you want)
private void Start()
{
if (SavedScore > HighScore)
{
HighScore = SavedScore;
}
}
DontDestroy
public int SavedScore = 0;
//that's all here, you don't want you persistent data holder to keep doing a bunch of stuff in other scenes, you just want it to be told what to hold by other scripts and you want other scripts to ask it what information it has, so your GAME LEVEL scene will tell this script what to hold, your MAIN MENU scene will ask what information this script contains.
Game Level (as your game updates its score, at some point you'll want it to tell the don't destroy script what the final score is, so either at death or even as it progresses)
public int CurrentScore = 0; (or public int CurrentScore;)
public void OnDestroy() (or game over or whatever instance you want to tell the don't destroy what the score is)
{
if (CurrentScore > SavedScore)
{
SavedScore = CurrentScore;
}
}
That's basically it, I would look up a tutorial on singularities or serialization or both. It will be super helpful as you go, but this is the basic gist. There's a few things I left out that you'll need from a serialization tutorial. If I typed it out this answer would be grossly longer. Best of Luck!
@surfuay Hello, thank you so much for the answer. Though this still wouldn't let me save from the other scenes as the scripts needed to reference are on the main menu scene.
no worries, the method I did was very long compared to the above answer. my method changed where you referenced things. you wouldn't have been referencing the main menu anymore, rather everything would reference the don't destroy object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211742im_/https://answers.unity.com/themes/thub/images/avi.jpg)