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 07:37 PM · playerprefssavestringload

PlayerPrefs script problem

Hi I've been at this all day an have gotten nowhere. I have a script for a message system and I've decides to use PlayerPrefs to save and load messages so they remain after the game has been closed and reopened. I get no script errors but for some reason when I close and open the app, posted messages are not reloaded. If anyone can tell me what's wrong with what i've done I'd be so grateful.

Here's the javascript code I've been working on:

 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];
     GetMessages();
 }
 
 function GetMessages(){
 if (PlayerPrefs.HasKey(nameField) == true)
 {
 PlayerPrefs.GetString(nameField, "Name");
 print("loaded");
 }
 }
 
 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);
         SaveString();
     }
     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 SaveString(){
 PlayerPrefs.SetString(nameField, "Name");
 print("saved");
 }

Thanks again!

Comment
Add comment · Show 2
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 DaveA · Aug 17, 2011 at 07:41 PM 0
Share

Without looking at your code yet, you do know there is a limit to how much you can store prefs, right? Is it possible you've gone beyond that limit?

avatar image blacksuit_films · Aug 17, 2011 at 07:53 PM 0
Share

It's okay I' know that there is a 1$$anonymous$$B limit. I have had no results as yet with this code. So far I have only used 4$$anonymous$$B of that space

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Mortennobel · Aug 17, 2011 at 07:57 PM

PlayerPrefs.GetString returns the value from PlayerPrefs.

See http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.GetString.html

Fixed code:

 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];
     GetMessages();
 }
 
 function GetMessages(){
 if (PlayerPrefs.HasKey(nameField) == true)
 {
 nameField = PlayerPrefs.GetString("Name");
 print("loaded");
 }
 }
 
 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);
         SaveString();
     }
     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 SaveString(){
 PlayerPrefs.SetString("Name",nameField);
 print("saved");
 }
Comment
Add comment · Show 20 · 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 08:04 PM 0
Share

GetString is currently in my code already within function Get$$anonymous$$essages.

avatar image Mortennobel · Aug 17, 2011 at 08:09 PM 0
Share

But you are not using what it returns. It should be something like: var result: String; result = PlayerPrefs.GetString(nameField);

Also - it does not make sence to use the second parameter (default value) when you check that the item do exist.

avatar image Mortennobel · Aug 17, 2011 at 08:13 PM 0
Share

Another issue - you are always saving the constant "Name" - that looks like a bug. I think you have swapped the parameters. Take a look at the documentation. (Read what the parameter are called)

avatar image blacksuit_films · Aug 17, 2011 at 08:14 PM 0
Share

Okay, I sortof see what you mean. I think my main misunderstanding here is the var and string at the beginning of your proposed code. what would I put in as the result for the var and string?

Apologies but filling in the blanks isn't something I'm good at.

avatar image blacksuit_films · Aug 17, 2011 at 08:19 PM 0
Share

I don't understand the documentation theres not enough info for a newbie. The only way for me to learn this properly is to see it done but there are no tutorials.

Show more comments
avatar image
0

Answer by blacksuit_films · Aug 17, 2011 at 08:46 PM

Gotcha... It says loadedName. I really appreciate you taking the time to help me here.

Comment
Add comment · 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

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

how to save a load a game? 1 Answer

PlayerPrefs Question 0 Answers

Save game support, how do I load audio in a sertain time frame? 0 Answers

How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers

There are wrong mathematical equations in PlayerPrefs. How can I resolve it? 1 Answer


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