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 dasani2406 · Sep 03, 2017 at 04:54 PM · listplayerprefsserializationstringbinaryformatter

How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization

I been searching this for a while and I am not moving forward at all. Any comment will help. So it you guys have any ideas just comment it. I tried to do one with playerPref, (Below is the code), BUT It is not working.

  public List<string> Levelnames= new List<string>();
 
 void Start()
 {
 for (int i = 0; i < Levelnames.Count; i++)
         {
             if (PlayerPrefs.HasKey("levels" + i))
             {
                 PlayerPrefs.GetString("levels" + i, Levelnames[i]);
                 print("load");
             }
 }
 
 void Update()
 {
  if (Input.GetKeyDown(KeyCode.Q))
         { 
  Levelnames.Add("levels");
 

 for (int i = 0; i < Levelnames.Count; i++)
             {
                 
                if (!PlayerPrefs.HasKey("levels" + i))
                 {
                    
                     PlayerPrefs.SetString("levels" + i, Levelnames[i]);
                   print("save");
               }
 
             }
        }
 }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bobisgod234 · Sep 04, 2017 at 06:49 AM

Perhaps something like this?

This add "levels" string to the Levelname list, and then save it to PlayerPrefs when you hit Q. When you start, it will load the previously saved levels.

         public List<string> Levelnames = new List<string>();
 
         void Start()
         {
             for (int i = 0; PlayerPrefs.HasKey("levels" + i); i++)
             {
                 Levelnames.Add(PlayerPrefs.GetString("levels" + i));
             }
         }
 
         void Update()
         {
             if (Input.GetKeyDown(KeyCode.Q))
             {
                 Levelnames.Add("levels");
 
                 for (int i = 0; i < Levelnames.Count; i++)
                 {
                     PlayerPrefs.SetString("levels" + i, Levelnames[i]);
                 }
             }
         }
Comment
Add comment · Show 3 · 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 dasani2406 · Sep 04, 2017 at 07:26 AM 0
Share

Hey Bob, Thank you a lot, I forgot to mention that the list already have 3 strings in it and the "levels" string is the one I want to add to the list and save it.

If I try the code your wrote with the 3 strings in the list, the 3 strings get repeated and then I get the "levels" string, which kind of make sense with the above code.

Now the problem is how do I prevent the 3 strings from repeating itself?

ALSO there will be other more strings that I would like to add in the list and save it.

avatar image bobisgod234 dasani2406 · Sep 04, 2017 at 07:40 AM 0
Share

the list already have 3 strings

How are these 3 strings added to the list? Are they just assigned in the inspector?

If so, try:

          public List<string> Levelnames = new List<string>();
          private int initialLength;
          void Start()
          {
              initialLength = Levelnames.Count;
              // only load extra entries, ignore any strings that were a
              for (int i = initialLength; PlayerPrefs.Has$$anonymous$$ey("levels" + i); i++)
              {
                  Levelnames.Add(PlayerPrefs.GetString("levels" + i));
              }
          }
  
          void Update()
          {
              if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Q))
              {
                  for (int i = initialLength; i < Levelnames.Count; i++)
                  {
                      PlayerPrefs.SetString("levels" + i, Levelnames[i]);
                  }
              }
          }


This will only load and save entries added after Start() is called. (Note: Untested).

avatar image dasani2406 bobisgod234 · Sep 04, 2017 at 06:55 PM 0
Share

Hey Bob, Yes, the 3 strings are assigned in the inspector. I tried the code above and It works..... I am able to save only the "levels" string and load it. It would be awesome if you could help me with one more thing.

If I want to add more strings in the list by pressing certain keys. Like pressing key E and I add one more string. Pressing key R and I add one more string and so on. How can I achieve that?

I do not want to do the whole thing again if I want the add another string to the list.

I know we need to change the "levels" string to something else, but I have no idea how to proceed from there.

Thank you for helping.

avatar image
0

Answer by jasonlu00 · Sep 03, 2017 at 06:04 PM

Since you didn't add any string to your Levelnames, Levelnames.Count is always zero. So you will never enter for loop. What you want to do is setup the list properly on Start:

 public List<string> Levelnames = new List<string>();
     private const int _numOfLevels = 10;
     private const string DEFAULT_NAME = "Unknown";
 
     void Start()
     {
         //Init Levelnames
         for(int i = 0; i < _numOfLevels; i++)
         {
             Levelnames.Add(PlayerPrefs.GetString("levels" + i, DEFAULT_NAME));
         }
     }
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Q))
         {
             for(int i = 0; i < Levelnames.Count; i++)
             {
                 PlayerPrefs.SetString("levels" + i, Levelnames[i]);
                 print("save");
             }
         }
     }
Comment
Add comment · Show 2 · 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 dasani2406 · Sep 04, 2017 at 06:14 AM 0
Share

Hey Jason, What does the "$$anonymous$$" means?

When we are adding: Levelnames.Add(PlayerPrefs.GetString("levels" + i, DEFAULT_NA$$anonymous$$E)); in Start(), are we really putting the save string or are we just adding a new string. I am a little confuse, can you explain a little. I am pretty new in this platform.

Thank you.

avatar image dasani2406 · Sep 04, 2017 at 06:26 AM 0
Share

Hey Jason, I updated my scripts, I forgot to include the "add string" in the update function, under the if statement.

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

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BinaryFormatter - saving and loading a list containing sprites. 0 Answers

Using BinaryFormatter for deserialization - ArgumentException: method argument length mismatch 0 Answers

A node in a childnode? 1 Answer

Is it possible to name a list by a string variable? 2 Answers

Using Player Prefs 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