- Home /
GUI Text Enter Name
Hi i am a total noob when it comes to scripting so i thought i would get my head around the gui text to start with.
i have this javascript attached to a camera.
var stringToEdit = "Player1";
function OnGUI () { GUI.Label (Rect (10, 10, 100, 30), "Enter Name:");
stringToEdit = GUI.TextField (Rect (90, 10, 200, 25), stringToEdit, 40);
}
what i would like to do is have the player enter his name then when the player pressed the return button it would take out the text field and change the GUI.Label to say "hello " stringToEdit " how are you." or something along those lines.
Please dumb it down as much as possible for me as im no good at scripting thanks!!
Answer by qJake · Mar 13, 2010 at 08:53 AM
Well, I'm not going to write the script out for you, but I can explain what you might want to try.
You would basically check to see if the user pressed enter using if(Input.GetKeyDown("enter"))
, and when that happened, set some sort of flag (just a true/false
that you keep at the very top of your script, outside of any functions). In your GUI function, you would check that flag. If it's false, display the text box. If it's true, display the label with the name in it. Something like this:
if(userHasPressedEnter)
{
// Draw "Hello" text here
}
else
{
// Draw GUI.TextField here
}
hey thanks, i did some nutting out with the way you said and this was my final script for anyone that cares.
var stringToEdit = "Player1"; var userHasHitOk : boolean = false;
function Update() { if(Input.Get$$anonymous$$eyDown("enter")) {
userHasHitOk = true; }
}
function OnGUI () {
if (userHasHitOk)
{
GUI.Label (Rect (10, 10, 100, 30), "Hello " + stringToEdit);
}
else
{
GUI.Label (Rect (10, 10, 100, 30), "Enter Name:");
stringToEdit = GUI.TextField (Rect (90, 10, 200, 25), stringToEdit, 40);
}
}
Answer by Bobby · Sep 24, 2010 at 05:59 PM
The Final script worked well, how can you then pass that name (stringToEdit) to another Scene.
For Example Alex collected all the Gems
Answer by Charles-Van-Norman · Feb 10, 2011 at 12:34 AM
To save something to another scene, use DontDestroyOnLoad. To save something even after the application quits, use PlayerPrefs.Set("name","Jacob") and PlayerPrefs.Get("name")
To make a text field go away when you press Enter, use:
[code]
using UnityEngine; using System.Collections;
public class example : MonoBehaviour {
bool userHasHitReturn = false; string stringToEdit = "";
void OnGUI() { Event e = Event.current; if (e.keyCode == KeyCode.Return) userHasHitReturn = true; else if (false == userHasHitReturn) stringToEdit = GUI.TextField(new Rect(0,0,100,50), stringToEdit, 25); } } [/code]