Enter Name in new UI

How to make enter name in new UI?

In old gui it was like:

Name.js
static var PlayerName : String = “Player”;

function OnGUI (){
PlayerName = GUI.TextArea (Rect(Screen.width / 2,10,100,27), PlayerName, 10);
if (GUI.Button (Rect (Screen.width / 2,45,100,25), “Login”)) {
Application.LoadLevel(“Test”);
}
}

And in next scene:

NameLoad.js
function OnGUI ()
{
GUI.Label (Rect (10, 100, 100, 20), "Welcome " + Name.PlayerName);
}

I dont know how to do that again in new UI, please help me.

First add an Event System game object to your scene. Then add a canvas. Set the canvas render mode to “Screen Space - Overlay”. Add a panel to the game object the canvas component is on. Then add the text control to the canvas.

Now that you have a text control, you must bind the change event to something so you can capture the value. Create a game object and attach a new script to it. In the script, create a function called something like OnTextChanged(string newValue). Go BACK to the editor, select the text control, scroll down the inspector and locate the “On value change” box. Click the “+” symbol on that to add an event row. Drag your game object over the [None (GameObject)] box. Now select your method (you can actually just use a public string if you’d like) under the DYNAMIC STRING section of the flyouts that show up.

Gotta love the new unity gui! This is why i’m writing my own UI library on codeplex :roll_eyes:

@LunaticEdit
I did it, but i still dont understand how this script should looks.

Name.js
function OnTextChanged(newValue){
}

And i dont know how to show this nick in next scene.

Im new in programming.

Basically you do the same thing as before but you are setting the .Text property of the text field, as follows:

var TextItem = GetComponent():
TextItem.Text = "Welcome " + Name.PlayerName

But as this is not the legacy GUI, you only set this in the start method of an attached script, or if it can change, do it in the Update method.

The other method that @LunaticEdit refers to is a pattern called data binding which is a bit more advanced but worth learning

Compiler errors like:
Assets/NameVar.js(1,35): BCE0043: Unexpected token: ).
Assets/NameVar.js(1,36): BCE0044: expecting ), found ‘:’.
Assets/NameVar.js(1,37): UCE0001: ‘;’ expected. Insert a semicolon at the end.

Please Help!

Ok, looks like I missed a semicolon at the end of that example :S, was typing from memory on my phone.

var TextItem = GetComponent<Text>();
TextItem.Text = "Welcome " + Name.PlayerName;

See if that helps

@SimonDarksideJ

Still this same compiller errors
Is it CSharp

Yes, that is Csharp code.
One minor change, now that I’m back at a PC :smile:
the Text property is all lower case, so should be:

var TextItem = GetComponent<Text>();
TextItem.text = "Welcome " + PlayerName;

In UnityScript it would be like this:

#pragma strict
static var PlayerName : String = "Player";

function Update () {

    var textItem : UnityEngine.UI.Text;
    textItem = gameObject.GetComponent(UnityEngine.UI.Text);
    textItem.text = "Welcome " + PlayerName;
}

I have made Canvas UI InputField and i dont know what should i add for it.

Your script is for next scene after name enter?

Debug:
NullReferenceException: Object reference not set to an instance of an object
NameSelect.Update () (at Assets/NameSelect.js:8)

Ok, for the input field, I would need to attach the above script to the PlaceHolder GO which has a text object attached to it.
That way you are showing a default before the player is typing anything.

@SimonDarksideJ Can u try to do that in my scene “Name”?
I really dont know how to do that.
http://www.filedropper.com/assets_5
Thanks in advance.

Right, understand your issue properly now :smile:
You want the player to enter their name in the InputField, then on clicking “Play Now” you want to grab the text they have entered to pass on to the rest of the game.

The InputField component has a “.text” property which holds the value entered, so you just need to grab that and put it in a static variable in a script that isn’t going to get destroyed (or do the poor mans way and store it in player prefs)

To get the entered text from the Input field, you simply:

public static string PlayerName;

// Update is called once per frame
public void SetPlayerName()
{
        var inputFieldGO = GameObject.FindGameObjectWithTag("InputField");
        var inputField = inputFieldGO.GetComponent<UnityEngine.UI.InputField>();
        PlayerNameManager.PlayerName = inputField.text;
}

I’ve simplified it so the script can be placed anywhere in the scene. Obviously you would need to tag the input field better.
The core part is discovering (or attaching the script to) the Input field GO and then grabbing the “InputField” component from it and interrogating the “.text” field to get the entered value.
Could be shortened to just:

GameObject.FindGameObjectWithTag("InputField").GetComponent<UnityEngine.UI.InputField>().text;

If you wanted it quicker.

Hope that helps
(Apologies for the delay getting back to you)

@SimonDarksideJ
Ehh…
Maybe i dont understand.

Assets/entered.cs(13,9): error CS0103: The name `PlayerNameManager’ does not exist in the current context

This script what u send to me i can place anywhere in scene?
What script i should add to Canvas Input Field?

Sorry, im new and i still dont understand,

Apologies, PlayerNameManager was a new script I created to show the example. Let me build it again and send it back to you.

@SimonDarksideJ Thank u :slight_smile: