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!!

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
}

The Final script worked well, how can you then pass that name (stringToEdit) to another Scene.

For Example Alex collected all the Gems

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:

</p>

<p>using UnityEngine;
using System.Collections;</p>

<p>public class example : MonoBehaviour
{</p>

<p>bool userHasHitReturn = false;
string stringToEdit = "";</p>

<p>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);
    }
}