Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Tommy · Mar 28, 2011 at 02:28 PM · guivariableplayerprefstextfield

Display Playerpref string in GUI.TextField.

Hey, i've got some trouble as im trying to display a Playerpref string in a gui textfield, heres the code:

var name1 : String = PlayerPrefs.GetString("Player1 Name"); var namevar1 : String;

function OnGUI () { GUI.Box (Rect (10, 40,500,500), ""); GUI.Label (Rect (20, 50,100,20), "Spelare #1 - "); namevar1 = GUI.TextField (Rect (90, 50,100,20), ""+name1); PlayerPrefs.SetString("Player1 Name", namevar1); //player1lvl = GUI.TextField (Rect (195, 50,100,20), ""+player1lvl); //PlayerPrefs.SetString("Player1 Level", player1lvl); //p1use = GUI.TextField (Rect (300, 50,15,20), ""+p1use); //PlayerPrefs.SetString("Player1 Use", p1use); }

Made lines that ain't relevant right now comments. Thanks in advance!

Current code:

var playerName : String;

function OnEnable() { playerName = PlayerPrefs.GetString("Player1 Name", "Player 1"); }

function OnDisable() { PlayerPrefs.SetString("Player1 Name", playerName); }

function OnGUI () { if (GUI.Button (Rect (10,10,100,20), "Instllningar")) showMoreGui = true;

 if (showMoreGui) { 
     GUI.Box (Rect (10, 40,500,500), ""); 
     GUI.Label (Rect (20, 50, 100, 20), "Spelare #1 - ");
     playerName = GUI.TextField (Rect (90, 50, 100, 20), playerName);

     if (GUI.Button (Rect (400, 510,100,20), "Stng")) 
         showMoreGui = false; 
 }

}

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
Best Answer

Answer by Tommy · Apr 07, 2011 at 12:58 PM

Here isa sulotion, that works just fine for me:

var namevar1 : String; var name1 : String;

function Update() { name1 = PlayerPrefs.GetString("Player1 Name"); namevar1 = name1; }

function OnGUI () { GUI.Box (Rect (10, 40,500,500), ""); GUI.Label (Rect (20, 50,100,20), "Spelare #1 - "); namevar1 = GUI.TextField (Rect (90, 50,100,20), ""+namevar1);

     if (GUI.changed)
 {
     PlayerPrefs.SetString("Player1 Name", namevar1);
 }

}

This will display the playerpref string "Player 1 Name", in the GUI textfield.

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
avatar image
2

Answer by Statement · Mar 28, 2011 at 02:50 PM

You aren't passing the same variable to the textfield as you get returned.

namevar1 = GUI.TextField (..., name1); // See that you use different variables?

Also, it is probably better to make load/save in OnEnable and OnDisable because I heard PlayerPrefs is somewhat slow. This also makes your GUI code much cleaner. Varsgod :)

var playerName : String;

function OnEnable() { playerName = PlayerPrefs.GetString("Player1 Name", "Player 1"); }

function OnDisable() { PlayerPrefs.SetString("Player1 Name", playerName); }

function OnGUI () { GUI.Box (Rect (10, 40, 500, 500), GUIContent.none); GUI.Label (Rect (20, 50, 100, 20), "Spelare #1 - "); playerName = GUI.TextField (Rect (90, 50, 100, 20), playerName); }

Comment
Add comment · Show 14 · 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 Tommy · Mar 28, 2011 at 03:02 PM 0
Share

I'm still trying to get this working for 18 objects that should do the same (18 textfields). Just woundering what the second var at the "playerName = PlayerPrefs.GetString("Player1 Name", "Player 1");" does? :)

avatar image Statement · Mar 28, 2011 at 03:05 PM 0
Share

The second variable "Player 1" is the default value returned if there was no value to start with. For example, playing the game for the first time. It's just a convenience so you don't have to write code such as if !exists player1, set player1 "Player 1". If you are going to repeat input like that I'd suggest you make a function that handles this automatically. Store each player value in a new class for easier variable management.

avatar image Statement · Mar 28, 2011 at 03:07 PM 0
Share

I suggest you make helper functions like LabelTextField which accept two parameters (except rect). Like so: LabelTextField(Rect (10, 40, 500, 500), "Spelare #1", player1.name);

avatar image Statement · Mar 28, 2011 at 03:08 PM 0
Share

And also make a higher level function like PlayerGUI(Rect(.....), player1) which in turn make use of LabelTextField, which you also have to code for yourself. $$anonymous$$uch cleaner than having a ton of controls right after each other :)

avatar image Statement · Mar 28, 2011 at 03:10 PM 0
Share

To group gui items make use of GUI.BeginGroup and GUI.EndGroup. That way you can work with relative position. There's also GUILayout so you don't have to deal a lot with rect sizes. Either way works of course and as long you get the job done any way is the right way :)

Show more comments

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

No one has followed this question yet.

Related Questions

C# How To Save PlayerPrefs for Unity GUI 2 Answers

GUI.TextField problem, won't update var. 0 Answers

Display additional text before the variable that the user is editing in a GUI Text Field 1 Answer

How to output variable in textfield? 1 Answer

How to make slider load variable on start 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