Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cdm89 · Sep 17, 2015 at 03:07 AM · c#save datastatic-variables

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cdm89 · Sep 28, 2015 at 12:31 AM 0
Share

@kingcoyote

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

how to Save A random generated number and use it 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges