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 blacksuit_films · Aug 17, 2011 at 10:28 AM · guiplayerprefssavestringloading

PlayerPrefs for saving strings

I'm trying out the PlayerPrefs Class with my code for a bulletin board now and I must be doing something wrong. I'm trying to load previously saved strings with OnLevelWasLoaded and then save new strings when the submit button is pressed. I can't seem to get it to work. I'd really appreciate it if someone could take a look at this.

   var stringToEdit: String = "What's The News?";
 var customSkin: GUISkin;
 var maxMessage = 10;
 var messagesTyped: String[];
 var messageSpacing: float = 10;
 var maxChar : int = 30;
 var scrollPosition : Vector2 = Vector2.zero;
 var messageAlert : String = "New Message From ";
 var nameField: String = "Name";
 private var msgCounter: int;
 
 function Start()
 {
     maxMessage--;
     messagesTyped = new String[maxMessage];
    
 }
 
 function OnLevelWasLoaded(){
     //get input
     loadLevel();
 }
 
 function loadLevel(){
     if(PlayerPrefs.HasKey(nameField+ stringToEdit)){
     Application.LoadLevel(PlayerPrefs.GetString(nameField, stringToEdit));
     }   
 }
 
 function OnGUI()
 {
     GUI.skin = customSkin;    
     
     stringToEdit = GUI.TextField(Rect(270, 100, 450, 80), stringToEdit, maxChar);
     nameField = GUI.TextField(Rect(80, 100, 180, 30), nameField, maxChar);
     if (GUI.Button(Rect(730, 100, 60, 30), "Submit"))
     {
         SubmitMsg( messageAlert + nameField + ":  " + stringToEdit);
         saveMessage();
         
     }
     var x: float = 270;
     var y: float = 180;
     for (var i: int;
     i < messagesTyped.length;
     i++)
     {
         y += messageSpacing;
         //print("Here");
         GUI.Label(Rect(x, y, 500, 60), messagesTyped[i]);
     }
     }
 function SubmitMsg(submittedMsg: String)
 {
     if (msgCounter == maxMessage)
     {
         msgCounter = 0;
     }
     messagesTyped[msgCounter] = submittedMsg;
     print(messagesTyped[0]);
     msgCounter++;
 }
 
 function saveMessage(){
 PlayerPrefs.SetString(nameField, stringToEdit);
 print("saved");
 }
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
Best Answer

Answer by Waz · Aug 17, 2011 at 12:40 PM

You're looking up a sum of two string when you check HasKey, but then you use a different parameter when you call GetString. That's why it's not working.

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 blacksuit_films · Aug 17, 2011 at 12:53 PM 0
Share

Thanks for the response. I've since changed that line to read:

if(PlayerPrefs.Has$$anonymous$$ey(nameField)){ Application.LoadLevel(PlayerPrefs.GetString(nameField, stringToEdit));

but now i'm getting an error message saying:

Level 'What's The News?' (-1) couldn't be loaded because it has not been added to the build settings. To add a level to the build settings use the menu File->Build Settings...

I havent heard anything about adding PlayerPrefs to build settings...or am I not supposed to call 'Application.LoadLevel' as part of loading these strings?

avatar image Waz · Aug 17, 2011 at 12:57 PM 0
Share

You're calling LoadLevel with that string, hence you get that result. I don't know what you expected to happen.

avatar image ChromeGames · Oct 11, 2012 at 08:07 PM 0
Share

Don't add Application.LoadLevel unless you are trying to load a scene. If so, just add it to the build settings.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs Question 0 Answers

reference variables within my xml file. 0 Answers

Loading a save/playerprefs from a different game? 2 Answers

PlayerPrefs Int to Float 1 Answer

Loading error when using PlayerPrefs.GetInt with WebPlayer 2 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