- Home /
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.
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
}
your answer uses GUI.box and my answer uses GUI.Text. so both solutions are possible.
@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.
Thanks for your answers and your comments. I'll try both out to see which one I most prefer.
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.
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.