- Home /
Keeping the int value "min" to be used in another scene.
I have one script in my first scene.
static public int min;
PlayerPrefs.SetInt("minv", min);
In the other scene I am trying to grab the int value however I only receive a 0.
public Text Test; public int min2; public string min2S;
void Update () {
min2 = PlayerPrefs.GetInt("minv");
min2S = min2.ToString();
Test.text = min2S;
I would like the value "min" from one scene to be printed in the Text "Test" in another scene. How would I go about doing this?
$$anonymous$$ake sure the PlayerPrefs call is actually called. Also make sure $$anonymous$$ is actually set to another value than 0.
Answer by PatrickDahlin · Apr 11, 2018 at 10:15 AM
PlayerPrefs does save information between scenes and sessions, as long as you remember to:
PlayerPrefs.Save();
This is most likely your problem since GetInt(..); returns 0 if the key is not found, which is the case if you didn't Save(); it.
Answer by tormentoarmagedoom · Apr 11, 2018 at 11:29 AM
Good day.
When a scene changes, Unity destroy all objects of the scene and instantiate the newones. So..
You need to preserve a GameObject with a Script between scenes (with all the information you want to preserve). Is not recommended to use PlayerPrefs for this. In all games, a lot of information is needed to be saved between scenes. For example in RPG games Player money, Player XP, player stats, etc...
What you are looking for is this:
DontDestroyOnLoad()
This property makes an object to dont be destroyed when changing scenes, so all values of all variables continue stored to be used.
Look for some tutorials if need more info, but is very simple to use.
If helped, accept the answer and close the issue. Thaanks!
Bye!!
Tho he'd be a solution he's using PlayerPrefs that is already a solution. So no need for DontDestroyOnLoad.
Using PlayerPrefs is not for this kind of data preservation. PlayerPrefs get stored in a deep C/: folder, to be used between sesions of the game. To save data when the game is closed, turn of the PC and load that data again when you open the game any time later.
Of course it will work, but he will be storing data outside the project folder and filling all users pc with "garbage"... Its 100% NOT RECO$$anonymous$$$$anonymous$$ENDED for this.