- Home /
Storing scene-specific variables?
Hi,
I'm trying to figure out the best way to store scene-specific variables. For instance, each scene has a specific music song. At the moment I have a gameManager object that controls the scene (and stores these variables, like what song should play in this scene).
The problem is that the gameManager is an istance of a prefab. I want to keep it a prefab, because i want the gameManager to change across all scenes if I add new functionality to it in the future. When I "apply" changes to this instance of gameManager, the music track variable will be set to this specific one too, across all scenes.
So what is the best way to store scene-specific variables?
Thank you very much in advance!
$$anonymous$$ake a constants file, that has a class for each scene maybe?
for example:
public static class Constants {
public static class Scene1 {
public int var1;
}
}
Constants.Scene1.var1 = 5;
Answer by Senuska · Aug 08, 2017 at 02:16 PM
From what I understand you have a singleton object that, among other things, functions as a music player (for lack of a better term). You can store all of the songs that are used in your project in an array on the gameManager, and have the gameManager check to see which of the scenes you are currently in, and play the appropriate song.
You should use the SceneManager to figure out which scene you are on, and use the sceneLoaded listener to add a function to select the correct song.
Your answer
Follow this Question
Related Questions
Instantiate new object from scratch 1 Answer
How to restart a "prefab'd" level? ;) 0 Answers
Prefab wont appear in game view but appears in scene view when called from CSV file. 2 Answers
Variable changes back to default after Start() 1 Answer
Are there restrictions on assigning GameObjects to Prefab script variables? 3 Answers