Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the "On Value Change" in Unity3D Input Field UI component

Tags:

c#

unity3d

I'm following a tutorial on how to work with Unity3d and I've hit a dead end.

I believe something changed in a newer version of Unity since the tutorial seems to work nice the way I'm doing it.

I have an Input Field UI component that I want to call a C# function every time I change it.

According to the tutorial I just have to use the "On Value Change" property of the Input Field (script) and tell it to call some function that takes a string as an argument.

public string playerName;
public void setName (string name)
{
    playerName = name;
    Debug.Log("Set playerName: "+name, gameObject);
    Debug.Log("Get playerName: "+playerName, gameObject);

}

Yet, this does nothing, my playerName property it's always empty and I don't receive anything in name.

How do I go about doing this? I've seen an answer setting up a listener in the Start() function, and then using an UnityEvent in here: Get text from Input field

But is there another way to do this using the Unity3d graphical editor that doesn't involve writing so much code?

like image 535
jbssm Avatar asked Mar 17 '15 23:03

jbssm


People also ask

How do you change the input field in unity?

Details. The Input Field script can be added to any existing Text control object from the menu (Component > UI > Input Field). Having done this, you should also drag the object to the Input Field's Text property to enable editing.

How do you change what is displayed on a UI text field through code unity?

1) Create a GameObject with a Text component; 2) Create a GameObject with a Button component; 3) Create a GameObject with a component of your custom script; 4) Create the reference in your custom script on the Text component you want to update; 5) Create a public method in your custom script that will be invoked when ...

How do I make a text box in unity?

To insert a Text UI element, go to the Scene Heirarchy, Create → UI → Text. A new Text element should show up in your Canvas region. If we have a look at its properties, we will see some very useful options.


1 Answers

Yes you can add event handlers via the inspector. Select the InputField game object and scroll down to the bottom of the InputField section in the inspector. Click + to add a new event handler then select the receiving game object and method you want to call.

Use the Dynamic string version of the function to pass the input string as a parameter. The Static Parameters callbacks let you set the function parameter in the inspector, which likely isn't what you want when responding to InputField changes.

enter image description here

like image 66
Huacanacha Avatar answered Oct 09 '22 09:10

Huacanacha