- Home /
How to pass string from input field using "OnSubmit (String)"
I'm using the new GUI system in the 4.6 Beta.
I've added a InputField UI element. I want to pass the string that is typed in the input field to the function I have created below. I've dragged the Gameobject containing the component with said function onto the On Submit (String) event, selected the public function below. The string isn't getting passed, also , the function isn't even being called.
Any Ideas?
public void SetClientAppName (string name){
Debug.Log("Called : " + name );
clientAppName = name;
RestartSyphon ();
}
I've also tried to just calling this...
public void SetClientAppName (){
Debug.Log("SetClientAppName Called" );
}
Without the string parameter, and it's still not getting called when text is entered.
Answer by harveyhb · Oct 17, 2014 at 07:16 PM
For anyone having this problem in the future.
On the InputField component, there is a checkbox for "Multi Line". Every time I pressed enter to submit text I was of course just starting a new line.
What this didn't solve is how to pass the string entered in the checkbox to my SetClientAppName function. I still haven't found a way to do this using OnSubmit (string) as this just passes whatever string is entered into the textbox to the right of the drop down to choose the function.
Instead I created a public input field and listener like so. Remember to include the "using UnityEngine.UI" header.
public InputField newClientName;
void Awake (){
newClientName.onSubmit.AddListener((value) => SetClientAppName(value));
}
public void SetClientAppName (string name){
Debug.Log("Called : " + name );
clientAppName = name;
RestartSyphon ();
}
Thanks for the code! I was browsing so many useless threads and video tutorials and none showed how to simply send the value, like you did in your Awake().
Answer by sollersstudios · Apr 23, 2015 at 01:33 PM
You can have the parameter of the function be a UI.Text component, and then add the reference of the same Text object as the value of the parameter your passing in.
public void UpdatedTextBox(UnityEngine.UI.Text newValue)
{
Debug.Log("Text was updated to: " + newValue.text);
}
Answer by jhocking · Nov 02, 2014 at 07:51 AM
As you already noticed, hitting Enter just creates a newline when the text is set to Multiline; according to threads on the forum, in the next version they added a selection for how to handle Enter.
As for the text being submitted, make sure to select function in top section "Dynamic string" and not bottom section "Static Parameters". I had the same issue at first and it took me a while to figure out; apparently you can use either dynamic or static string parameters and I had selected the function in static.
Your answer
![](https://koobas.hobune.stream/wayback/20220613165313im_/https://answers.unity.com/themes/thub/images/avi.jpg)