I have a script called dataSheet attached to the GameObject DataSheet. How do I access booleans in it from another scene?
Asked
Active
Viewed 621 times
-1
-
From another scene or another GameObject? You can't access anything from another scene – Programmer Jan 17 '17 at 14:02
-
@Programmer I have a scene with main menu and there are some booleans, for instance, "hints", and I need to access them from another scene to make it show hints or not – Sharpy Jan 17 '17 at 14:10
-
Just to let you know why you are getting down-voted in your questions. You should say what you are doing, your problem and what you expect to happen. You should also mention what you have tried. If you ask one or two line question that requires people to ask for more info, you will likely be down-voted with the question closed...A reference for your future questions. – Programmer Jan 17 '17 at 16:27
2 Answers
3
When you load a new scene, Unity destroys all objects of the old scene before it creates the objects of the new scene.
You have to mark objects from the previous scene by calling DontDestroyOnLoad()
But this is not right approach how to work with Unity

Erik Šťastný
- 1,487
- 1
- 15
- 41
-
-
2I wouldn't call this the wrong approach. This is how I'd do it. It's scalable and manageble where PlayerPrefs are not. – Fredrik Schön Jan 17 '17 at 14:24
-
-
1You are limited to SetInt, SetFloat, SetString and they will be spread all over your solution. A persistant DDOL-game manager class will support any types and can contain logic if needed that doesn't clutter your code everywhere. Perhaps it's just personal preference but if you ever need more than a couple of bools from scene to scene, I'd use some kind of static game manager. This is what I *believe*, and I'd be very happy to be corrected if I'm wrong! – Fredrik Schön Jan 17 '17 at 14:31
-
A downside would be that you have to find the gameobject and the script-component in every script that you need it (if you need the MonoBehaviour). Otherwise a static class should suffice. – Fredrik Schön Jan 17 '17 at 14:34
-
There is nothing wrong with your answer, it's great for its purpose! You already have my upvote. I just prefer more strongly typed code. – Fredrik Schön Jan 17 '17 at 14:35
-
You can serialize a class and save it as string64. So you are not limited to 3 types. You can also extend the class to perform more specific actions. In my answer, a boolean is stored as int, I could provide a method Set/GetBoolean to wrap that. – Everts Jan 17 '17 at 15:05
-
@Fredrik You can make a bool [wrapper](http://stackoverflow.com/q/41073236/3785314) like Evert said. – Programmer Jan 17 '17 at 16:17
-
I understand that it can be done, I'm just saying it's not a good solution. You can store ints that translate to any code if handled. Are you saying it's better solution than creating a static data-holder class? – Fredrik Schön Jan 17 '17 at 17:14
3
You have a class you want to save the boolean:
private bool myBool = false;
void LoadingNewScene()
{
int i = (mybool == true) ? 1 : 0;
PlayerPrefs.SetInt("TheValue", i);
}
The above assumes that LoadingNewScene is called when loading a new scene.tThen you have a class that is attached to a game object in the other scene:
void Awake(){
if(PlayerPrefs.HasKey("TheValue") == true){
bool result = PlayerPrefs.GetInt("TheValue") > 0;
}
}
You can also use the MonoBehaviour callback such as OnDestroy or the SceneManager event system.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

Everts
- 10,408
- 2
- 34
- 45
-
I dream PlayerPrefs stop being used for everything and nothing.... It's called **Player Preferences** for a reason. It's not designed to store data about the game itself. – Hellium Jan 17 '17 at 14:55
-
Well you should not stop dreaming coz they are widely used for that "wrong" purpose. Whether you save player data or game data does not matter. They're just data. But wava. – Everts Jan 17 '17 at 15:10
-
PlayerPrefs are stored in the registry and thus, can be easily modified by the users, and produce garbage for the computer. I think it's better to prevent the beginners from using Player Prefs and indicate them the good way to go. – Hellium Jan 17 '17 at 15:14
-
@Hellium Sorry, I disagree with you and Fredrik's comments. "can be easily modified by the users, and produce garbage for the computer" I have seen this argument before but it is a poor one. As long as it is saved on the Player's computer, they can easily modify it. As for garbage for the computer, that's false, it does not. It does not write to the important OS registry paths. It writes to Application registry. It even writes under the GameName under Application registry. If you don't want it, you can go there and delete the GameName registry and it will be gone. – Programmer Jan 17 '17 at 15:41
-
I've seen people saying the-same thing but they absolutely have no idea what they are talking about. It does not mess the System registry where OS saves its important state. It saves to the GameName key which in under Application registry and that is totally fine. Not to mention you can use `PlayerPrefs.DeleteAll` to clear them out. – Programmer Jan 17 '17 at 15:45
-
@Programmer : You have all my respect if you are able to read a binary file or a file with custom structure and encryption. And yes, that's what I meant when I told "It produces garbage for the computer", it clutters your application registry so as to save a high score. – Hellium Jan 17 '17 at 15:45
-
In fact, stuff like high scores(int), booleans are made to be saved with `PlayerPrefs`. They are all basic types. Remember that this question is talking about boolean. The time it becomes a no no to use `PlayerPrefs` is when you are saving an extremely long string or bytes such as an image or sound data. Finally, please *remember that the registry is also a group of files*. The stuff you save are not scattered everywhere. They are put in one spot and you can get ride of them anytime with PlayerPrefs.DeleteAll or by simply deleting the key. Sorry for long post and happy coding! – Programmer Jan 17 '17 at 16:04