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
0
Question by randombinaries · Apr 11, 2012 at 12:24 PM · javascriptstringguitextlabel

How do you display a saved name on the screen?

I've got a script that enables the player to type in their name, which saves and stores it using playerprefs as a string. As soon as the player plays the scene again that saved name is then displayed in the console. I don't really want to display in the debug console because the gamer cannot see their name on the screen. How would I go about displaying that name on the screen either using a label or GUItext? Here's my code:

var PlayerNAME : String = "";

var SavedNAME;

function Start() {

 SavedNAME = PlayerPrefs.GetString("", PlayerNAME);
 if(SavedNAME != null) {
     print(SavedNAME);
 }

}

function OnGUI () {

 GUI.Label(new Rect (10, 10, 50, 25), "Name:");
 PlayerNAME = GUI.TextArea(new Rect(65, 10, 100, 25), PlayerNAME, 10);
 
  if (GUI.Button(Rect(10,70,50,30),"Click")) {
     SaveName(PlayerNAME);
  }

}

function SaveName(PlayerNAME : String) {

 PlayerPrefs.SetString("", PlayerNAME);

}

Could you please leave sample code to explain your solution to my problem. Thank you for your help.

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 AlucardJay · Apr 11, 2012 at 01:17 PM

Both the question and the other answer slightly confused me , but I think this is what you want :

 #pragma strict
 
 var PlayerNAME : String = "";
 var SavedNAME : String;
 
 function Start() 
 {
     SavedNAME = PlayerPrefs.GetString("Stored Name", PlayerNAME);
     
     if(SavedNAME != null) 
     {
         print(SavedNAME);
         PlayerNAME = SavedNAME; // show the current SavedNAME in the GUI.TextArea
     }
 }
 
 function OnGUI() 
 {
     GUI.Label(Rect (10, 10, 50, 25), "Name:");
     PlayerNAME = GUI.TextArea(Rect(65, 10, 100, 25), PlayerNAME, 10);
     
      if (GUI.Button(Rect(10, 70, 50, 30), "Click")) 
      {
         SaveName(PlayerNAME);
      }
     
     // Show the Current SavedNAME
     GUI.Box(Rect((Screen.width/2)-100, 10, 200, 30), "Hello " + SavedNAME); // Text in Box
     // GUI.Label(Rect((Screen.width/2)-100, 10, 200, 30), "Hello " + SavedNAME); // Text as Label
 }
 
 function SaveName(PlayerNAME : String) 
 {
     PlayerPrefs.SetString("Stored Name", PlayerNAME);
     SavedNAME = PlayerNAME; // reset SavedNAME to the PlayerNAME from GUI.TextArea and GUI.Button
 } 
Comment
Add comment · Show 4 · 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 ExTheSea · Apr 11, 2012 at 01:47 PM 0
Share

your answer uses GUI.box and my answer uses GUI.Text. so both solutions are possible.

avatar image AlucardJay · Apr 11, 2012 at 02:06 PM 1
Share

@ExTheSea , as I said, "Both the question and the other answer slightly confused me".

I imagine anyone else reading it would feel the same. $$anonymous$$y answer was only to help @randombinaries , as they asked "Could you please leave sample code to explain your solution to my problem" , so I displayed the full script with comments so they could follow the process. (and I did it without creating a new variable , which yours is incorrect, as $$anonymous$$leptomaniac pointed out)

Basically , I didn't understand what you were saying. I really don't $$anonymous$$d who gets credit for the answer , as long as @randombinaries gets an answer they can understand and learn from.

GUI.Box and GUI.Label is just a $$anonymous$$or detail in this solution.

avatar image randombinaries · Apr 11, 2012 at 07:18 PM 0
Share

Thanks for your answers and your comments. I'll try both out to see which one I most prefer.

avatar image randombinaries · Apr 11, 2012 at 07:25 PM 0
Share

How could you use the above two solutions to record and display each player entry? The problem that I think could arise from the solutions is that as soon as a new entry is displayed on the screen. It could overwrite the older entry whereas I need it to display all entries whether it is old or not.

avatar image
0

Answer by ExTheSea · Apr 11, 2012 at 12:46 PM

I would add a GUI.Text and called it like "NameGUI" or something like this. then i would add to your script:

var NameDisplay : GUIText;

Then you can edit the NameDisplay like so:

NameDisplay = PlayerNAME; Put this code in your OnGUI and after you saved, drag the NameGUI onto your NameDisplay variable.

Comment
Add comment · Show 1 · 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 Kleptomaniac · Apr 11, 2012 at 02:00 PM 0
Share

Needs to be NameDisplay.text = PlayerName however.

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

6 People are following this question.

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

Related Questions

How to display previous players scores as well as current player score at the end of the game? 0 Answers

How to make GUI text disappear after a few seconds? 1 Answer

How can i split a string[] ? 1 Answer

Sending a string via email from Unity? 2 Answers

A node in a childnode? 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