system
April 11, 2012, 12:24pm
1
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.
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
}
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.