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?
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.
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 ...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With