8

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?

Community
  • 1
  • 1
jbssm
  • 6,861
  • 13
  • 54
  • 81

2 Answers2

9

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

Huacanacha
  • 1,151
  • 7
  • 9
  • 2
    Thank you, this was what I was doing, but then I found I need to use the `Dynamic String` assignment and not the `Static Parameters` to choose my function. I still don't know exactly why, but that made it work. – jbssm Mar 18 '15 at 12:09
5

with your comment you just made it all clear. When you use the Dynamic Parameter, the value of your input entry is passed to your function. When you choose the Static Parameter, a new input box is shown in the inspector, for you to write the "static parameter" you want to receive.

Not sure yet how this is useful. But it should make clear the difference.

Alberto
  • 499
  • 4
  • 23
  • 1
    this isn't really an answer, it's a comment, but your answer made it clear that there is a label inside the AddListener Dropdown which labels two types of function Dynamic Parameter and Static Parameter – johnny 5 Jan 21 '17 at 04:39