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 /
This question was closed Aug 18, 2011 at 02:44 PM by Joshua for the following reason:

The question is answered

avatar image
0
Question by blacksuit_films · Aug 17, 2011 at 10:39 PM · playerprefssavestringloading

PlayerPrefs Question

I'm trying to build a message box and am using PlayerPrefs to save new messages and reload them after the app is closed and reopened. However i've ended up having the namefield load up rather than the message. I've taken a closer look and can see why this has happened and it seems I need to change the variable from nameField to something like messagesTyped.

The problem is the messagesTyped variable I currently have won't work with the SetString and GetString commands. I was wondering if anyone has any suggestions for how I can make this work. Should I create a new variable to signal the messages submitted with the submit button and use that with the PlayerPrefs?

Here's the code as it is at the moment:

 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"+nameField);
 }
 }
 
 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"+nameField);
 }
Comment
Add comment · Show 6
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 Waz · Aug 17, 2011 at 11:47 PM 4
Share

These are beco$$anonymous$$g a set of "please write my script for me" questions. Do you actually understand what is already in that script? It seems that you have found something somewhere and are trying to hack it to serve some other purpose, rather than learning how it works and then modifying it to your needs.

You should at least understand enough to write your question, which I'm just guessing is: "How do I store an array of String in PlayerPrefs?", which you can find an answer for by just typing "playerprefs array string" into the search box above.

When you do not understand "your own" code, you can't ask answerable questions, so you end up with "Here is a script, please fix it", which is just junk on this site, since it will never be useful to anyone but yourself.

avatar image blacksuit_films · Aug 18, 2011 at 10:25 AM 0
Share

Erm...okay. Either way you answered my question. So thanks anyway angry person

avatar image Waz · Aug 18, 2011 at 10:50 AM 0
Share

It's a tough job trying to $$anonymous$$ch fishing to people that just want a fish, already!

avatar image blacksuit_films · Aug 18, 2011 at 11:26 AM 0
Share

Enjoyable metaphors aside. I can see code anywhere (especially on the wiki page) but if you're very new to this and trying to learn, none of it is substantially is explained.

I have written quite a lot of this code above but yes some people have helped me out immensely along the way and I have learned a great deal from them. However arrays remain a sticking point, remember this aint easy for everyone.

Aaaanyway, off back to the grind. Cheers anyway

avatar image Waz · Aug 18, 2011 at 12:14 PM 0
Share

I'm just grumpy from the Influenza I'm suffering from at the moment. I hate Winter.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs for saving strings 1 Answer

PlayerPrefs script problem 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Storing in PlayerPref based on timer 1 Answer

Saving a button in PlayerPrefs 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