- Home /
How can you save and update data between scenes and upon log in?
Hello, I am new to scripting and unity3d. I am still trying to understand OOP as well.
My issue: I have a static class that i am using to save data - specifically the following: Username and the amount of gold that user has. I am calling it LevelManager I can't figure out what the best way to do this is.
LevelManager
public class LevelManager
{
public static LevelManager lvlManager = new LevelManager();
public static void LoadLevel(string name)
{
Debug.Log ("New Level Load: " + name);
Application.LoadLevel (name);
}
public static void QuitRequest()
{
Debug.Log ("Quit requested");
Application.Quit ();
}
public static void initalize( ref string Name,
ref string Name2,
ref int admin_Gold,
ref int admin_Gold1)
{
Name = "Admin";
Name2 = "Admin1";
admin_Gold = 1000;
admin_Gold1 = 500;
}
}
In A different class that I created for the Login Screen I am calling the LeveManager.intalize Method this class is called:
LoginManager
public class LoginManager : MonoBehaviour {
public string userName;
public string passWord;
public Image loginButton;
public Image RepairButton;
public Image SellButton;
public string str1;
public string str2;
public int int1;
public int int2;
public void LoginFunction()
{
userName = GameObject.Find ("UserName").GetComponent<InputField>().text;
passWord = GameObject.Find ("Password").GetComponent<InputField>().text;
LevelManager.initalize( ref str1,
ref str2,
ref int1,
ref int2);
string UserName = str1;
string UserName2 = str2;
if (userName == UserName && passWord == "password"
|| userName == UserName2 && passWord == "password1")
{
Debug.Log("the user name" + UserName);
Application.LoadLevel("MainMenu");
}else{
Debug.Log("Else Statement");
Debug.Log(userName + passWord);
};
}
} I want to take the username and be able to save it back to the static class as a value. On another class (that i have not created yet) I want to be able to update the value for gold and save it as well. Does anyone have any recommendations? What is this kind of idea called in OOP?
Answer by kingcoyote · Sep 17, 2015 at 06:09 AM
Looks like you want to use the PlayerPrefs class. It allows for persistent storage between game sessions. You can write values to PlayerPrefs using a few different commands:
PlayerPrefs.SetInt("Gold", 1500);
PlayerPrefs.SetString("Name", "Admin");
or read from PlayerPrefs using other commands:
Name = PlayerPrefs.GetString("Name");
Gold = PlayerPrefs.GetInt("Gold");
The Get commands take an extra optional parameter that is the default value. That works to return a value if one hasn't already been stored. Say you want to get the gold amount, and return 1500 the first time when gold hasn't been set:
Gold = PlayerPrefs.GetInt("Gold", 1500);
Thank you so much for your answer this is very helpful. I was wondering though, lets say in the future i wanted this code to exist on a server, could i still use playerprefs? - is it ideal to build my own static class? (As i was trying to do originally) and if the answer is yes. what would i use to achieve this?
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220612033757im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to create log by PlayerPrefs? 1 Answer
Save Data on iOS 0 Answers