To those who are saying that it's not working- when selecting ReadStringInput in the Input Field, make sure its dynamic and not static (there should be two sections).
we moved the content in english to another channel: www.youtube.com/@GameDevTraumEN/videos please consider subscribing to it ❤ if you have questions about unity or programming just leave a comment
Thanks a lot! Your video helped immensely where the Unity Docs did not, particularly for the difference between the OnValueChanged and OnEndEdit events, and their ties (or not) to scripts!. And the comment by @Lorenzo Bergamini and your reply (about making sure to use the upper elements of the list that are Dynamic, which you stated at 2:23, but which I also missed when initially watching the video. Doh!) was spot on! Well done!
Excellent video! I struggled to understand how to compare 2 fields (Password and Confirm Password) text string fields, and this tutorial got me exactly where I needed to be. THANK YOU!
My problem is if i use input field for entering in messages for a chat, the text goes outside of the background input field and I have moved the text and it doesn't resolve the issue.
i have a problem, this worked for me but after i tweaked some stuff it suddenly didnt work and i dont know why. When i type into the input, the debug log will slow empty, but there is stuff in my input when i go back. that means the string isnt working right? something wrong with my ReadStringInput? but idk why it worked before then stopped, its same as ur code
Nice explanation, but it doesn't work form me :( A text box appears in the inspector (in the OnEndEdit box) and in the console is shown what I write in the inspector, instead of what a I wirte in the InputField. Do you know why?
Probably you selected the static function in the OnValueChanged or the OnEndEdit event, for the static function you have to write the string in the inspector to execute the function and always is executed with that same value. The dynamic function instead sends to the function the string that is written in the Input field
@@lorenzobergamini4657 I do it in 2:23, the upper elements of that list are the dynamic functions (note the tittle "Dynamic Strings"), while the elements below are the static functions, Our function is in both lists but in this case you have to select the one that is in the Dynamic Strings list
mmm there are several alternatives depending on ui design and target platform, if you are in a PC build maybe you want to display a message indicating that the input wasn't correct, maybe a GameObject in the canvas with a background panel, a text with the proper message and an OK Button that simply closes the message it self, for that in the On Click event of the button, click on plus, assign the parent of the message and in the drop down menu select "GameObject" - "SetActive(bool)" and uncheck the checkbox, this way when you press the button the panel closes. Once you have the message working, then work in the logic of the Input Field, suppose you have a script and inside you define a function that is going to be called on the "On End Edit" event from the Input field, something like this": public void InputFieldEnter(string s){ //The user entered a text and hits enter, "s" is the entered text } Connect this function to the Input field, similar to the button, in the On End Edit event from the InputField, click on plus, drag the GameObject that has the script with the function and select it using the drop down menu (make sure you use the function listed on "Dynamic", that way the value send to the function is the content of the input field). Now for the logic, if the input field is empty, display the message else proceed with the next part, something like this: public void InputFieldEnter(string s){ if(s == ""){ errorMessage.SetActive(true); }else{ // do whatever you want if the input is correct } } In the previous case "errorMessage" is a GameObject type variable that has to be defined below the "Start" method and you need to make sure tu drag the GameObject of the error message in the field in the inspector, you define the variable like this: public GameObject errorMessage; //Drag the error message in this field in the inspector So, in summary, if you solve the problem this way, when you enters a null value in the input field, the message will appear, you click OK and enters the text again.
this only works if you have 1 input, what if you have multiple inputs and want to reuse the ReadStringInput functions? how does the code know which input to update?
yes, but you need to define the reference for an InputField component in your script, you can do it this defining a variable above the Start method for example: public InputField theInputFieldIWantToClear; If the prevous line gives you an error, prabably you need to import the proper namespace, above all add: using UnityEngine.UI; Then itˋs very important to initialize that variable with the component you want to clear, if you don´t do this step you´ll get a null reference exception. A simple way to initialize the variable is assigning the GameObject with the InputField component to the field called "theInputFieldIWantToClear" in the inspector of your script. You have a video here: th-cam.com/video/ESwadW9Ttzg/w-d-xo.html Finally the clear part, for that you can do: theInputFieldIWantToClear.text = ""; You can add the previous line were you need to clear the input field, for example in the OnEndEdit function that executes the InputField
hahah xD I'll try to wake up earlier in the next video btw I'm moving the new content in english to this other channel: th-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.htmlvideos
@@GameDevTraum Yup :( I an manually assign the var a value and it shows. But it isn't based on the user input field. Tried some other things and same result... hummm edit: imgur.com/a/egOn0or :) Edit2: I have also just tried to do debug.log(s); and same result... not have much luck reading these input fields
@@MoonLiteNite in the on end edit you have to look closely... there is a static area and a dynamic area.. both have the function in it. It seems you have selected the static one.
yes of course! mmm you can do it in different ways, for example, make each field execute it's own function inside the script (in the on end edit event of each input field), then add a button that executes another function that use the information entered. This could be a code for that: //Fields string info1;//the info send by the input field 1 string info2; string info3; //Methods //The next 3 functions are executed in the on end edit of each input field //use methods listed in "dynamic strings" public void SetInput1(string s){ info1=s; } public void SetInput2(string s){ info2=s; } public void SetInput3(string s){ info3=s; } public void ExecuteSomeFunction(){ //this function is executed by a button //use info1, info2 and info3 to do whatever you want } Another alternative is perform a function when all the input fields aren't empty, for example: public void SetInput1(string s){ info1=s; if(info1!="" &&info2!="" &&info3!=""){ ExecuteSomeFunction(); } } public void SetInput2(string s){ info2=s; if(info1!="" &&info2!="" &&info3!=""){ ExecuteSomeFunction(); } } public void SetInput3(string s){ info3=s; if(info1!="" &&info2!="" &&info3!=""){ ExecuteSomeFunction(); } } public void ExecuteSomeFunction(){ //this function is executed by a button //use info1, info2 and info3 to do whatever you want }
heey, first let me tell you that I have anohter channel where I will be uploading the upcoming content in english (the one with the blue icon): th-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.htmlvideos Please consider subscribing to it. About your problem, let's divide it in two parts. first saving and loading a string, you can find info about saving strings with player prefs here: gamedevtraum.com/en/game-development/unity-tutorials-and-solutions/how-to-save-and-load-a-string-with-playerprefs-in-unity/ I suggest you create two functions, one to load and one to save, suppose you define a variable called "inputSavedData": public string inputSavedData; public void SaveData(){ PlayerPrefs.SetString(“aNameForYourData”,inputSavedData); } public void LoadData(){ inputSavedData = PlayerPrefs. GetString(“aNameForYourData”,””); } you can call LoadData in Start or Awake functions, like this: void Start(){ LoadData(); } And the saveData function you can call it where you read the input field, let's suppose you do that in a function called, InputFieldFunction: public void InputFieldFunction(string inputText){ inputSavedData = inputText; SaveData(); //do whatever you need } So up until here is the first part of the problem where you save the data and then recover it. Now to write the saved text in the input field when loading, for that you'll need to define a reference for an InputField type component, first make sure you implement the UI namespace, above all the script add: using UnityEngine.UI; then above start you can define: public InputField myInputField; And then in Unity drag the InputField GameObject to the "myInputField" variable in the inspector of the script. Once this is done you can write a text in the input field, so once you load the data, you can write the recover text to the input field: void Start(){ LoadData(); myInputField.text = inputSavedData; } I hope you can solve your problem
Thank you for your effort, that was very helpful. but what if I have two inputfields that I want to read and want to create a separate string for each inputfield? is that possible?
Yes of course, everything is possible :D Just make two sepparate functions in your script and connect each input field to each function just how it's done in this video. Inside the function you can make a boolean variable true indicating that the field was entered, and then check if the other input field is also entered and make things happen in that case. This could be an example: //Here Begins C# bool input1Ready=false; bool input2Ready=false; string input1Value; string input2Value; public void FunctionForInputField1(string textFromInput){ //Connect this function to one input field input1Value=textFromInput; input1Ready=true; if(input1Ready && input2Ready){ //both inputs are entered, what should we do? } } public void FunctionForInputField2(string textFromInput){ //Connect this function to another input field input2Value=textFromInput; input2Ready=true; if(input1Ready && input2Ready){ //both inputs are entered, what should we do? } } //Here Ends C# btw dein Name klingt super Deutsch aus xD
Great video, thank you so much! I am trying to make a search tab in unity, where once I type something in the input field it checks it against my objects (courses in my case) and if there is a specific course with the name in my scene, then it will print it. How should I go about this?
How do I receive an integer input? Does it work if I just set content type in input field settings to "Integer Number" and then change the variable type in reading method to "int"?
I haven't try doing that, but you can parse the string value to int, I think the instruction is this: int.Parse(stringValue); I'll check in a couple hours
Yep, the InputField sends a string to the selected function inside the script, then you have to convert it to int. For example this way: int intValue = int.Parse(stringValue); intValue can also be defined as a global variable. You also have the instruction: int.TryParse(stringValue) Which returns a boolean indicating weather the convertion was successful or not, in case the user writes text instead of numbers for example
@@GameDevTraum i have a question: for some reason when i try the parse thing that you did it resets my scene and i get errors saying the the value is not in the correct format. I would like to know what i did wrong. here is a copy of the section that gives errors: void Update() { if (enemiescount == null){ } if (enemiescount1 == null){ } if (enemiescount2 == null){ } if (enemiescount3 == null){ } if (enemiescount != null){ int enemycounter = int.Parse(enemiescount); } if (enemiescount1 != null){ int enemycounter1 = int.Parse(enemiescount1); } if (enemiescount2 != null){ int enemycounter2 = int.Parse(enemiescount2); } if (enemiescount3 != null){ int enemycounter3 = int.Parse(enemiescount3); } } enemiescount(1-4) is the string that i am trying to convert into a integer so i can use that string to change a integer variable in another script as i am trying to make a sandbox mode for my game where i can type a number into an input field then that number will be the amount of enemies that spawn. i don't suppose you could help me with that?
¡Hi GameDevTraum! When I finished watching your video, a question arose in me. Which functions the input field controls when an area of text is highlighted, for example to bold part of a dialog?
I have another channel, please SUBSCRIBE here 😄👉 www.youtube.com/@GameDevTraumDE
*LEARN more UNITY here* 👉 th-cam.com/video/E8-k9_DdkBg/w-d-xo.html
To those who are saying that it's not working- when selecting ReadStringInput in the Input Field, make sure its dynamic and not static (there should be two sections).
Yea this is easy to miss.
I just spent 20 minutes trying to solve and I did. Was going to write a comment and saw this at top. I should have read comments first haha
we moved the content in english to another channel:
www.youtube.com/@GameDevTraumEN/videos
please consider subscribing to it ❤
if you have questions about unity or programming just leave a comment
Thanks you bro!
Youre a lifesaver! Thank you!
Thanks, finally. I searched for a lot long useless things but finally found that
Thanks a lot! Your video helped immensely where the Unity Docs did not, particularly for the difference between the OnValueChanged and OnEndEdit events, and their ties (or not) to scripts!. And the comment by @Lorenzo Bergamini and your reply (about making sure to use the upper elements of the list that are Dynamic, which you stated at 2:23, but which I also missed when initially watching the video. Doh!) was spot on! Well done!
Great!! I'm glad that helped 😄😄
DUDE, LOVE YOU, FAST AND TO THE POINT, FINALLY. THANKS
Fast and to the point 👌 gotta love a well rehearsed and spoken video
Excellent video! I struggled to understand how to compare 2 fields (Password and Confirm Password) text string fields, and this tutorial got me exactly where I needed to be.
THANK YOU!
how would i use their text in a if statement?
Very well explained and under 4 min!
Thank you so much! Short and clear.
you are a god amongst men my friend
Hahaha amazing!!
If it dosen't work and there is another input area in the OnEndEdit box make sure you chose the function under dianamic(Sorry forgot the spelling)
thank u
My problem is if i use input field for entering in messages for a chat, the text goes outside of the background input field and I have moved the text and it doesn't resolve the issue.
Thats exactly what i needed thank you!
i have a problem, this worked for me but after i tweaked some stuff it suddenly didnt work and i dont know why. When i type into the input, the debug log will slow empty, but there is stuff in my input when i go back. that means the string isnt working right? something wrong with my ReadStringInput? but idk why it worked before then stopped, its same as ur code
Heey, make sure you choose the dynamic string function in the the drop-down menu of the on end edit event
@@GameDevTraum oh i see. Ill take note of that next time ty! In the end i went back to my last version and continued from there
Thank you so much for making this tutorial!!
Thank you for simple and instructive video.
Nice explanation, but it doesn't work form me :( A text box appears in the inspector (in the OnEndEdit box) and in the console is shown what I write in the inspector, instead of what a I wirte in the InputField. Do you know why?
Probably you selected the static function in the OnValueChanged or the OnEndEdit event, for the static function you have to write the string in the inspector to execute the function and always is executed with that same value. The dynamic function instead sends to the function the string that is written in the Input field
@@GameDevTraum How can I select the dynamic function? I can't see any setting to change it
@@lorenzobergamini4657 I do it in 2:23, the upper elements of that list are the dynamic functions (note the tittle "Dynamic Strings"), while the elements below are the static functions, Our function is in both lists but in this case you have to select the one that is in the Dynamic Strings list
@@GameDevTraum Oh I didn't even notice that, thank you very much!
Quick and easy to understand! Thanks!
How do I validate the input field? More specifically if it's an empty field (null) prompt the player to input text again?
mmm there are several alternatives depending on ui design and target platform, if you are in a PC build maybe you want to display a message indicating that the input wasn't correct, maybe a GameObject in the canvas with a background panel, a text with the proper message and an OK Button that simply closes the message it self, for that in the On Click event of the button, click on plus, assign the parent of the message and in the drop down menu select "GameObject" - "SetActive(bool)" and uncheck the checkbox, this way when you press the button the panel closes.
Once you have the message working, then work in the logic of the Input Field, suppose you have a script and inside you define a function that is going to be called on the "On End Edit" event from the Input field, something like this":
public void InputFieldEnter(string s){
//The user entered a text and hits enter, "s" is the entered text
}
Connect this function to the Input field, similar to the button, in the On End Edit event from the InputField, click on plus, drag the GameObject that has the script with the function and select it using the drop down menu (make sure you use the function listed on "Dynamic", that way the value send to the function is the content of the input field).
Now for the logic, if the input field is empty, display the message else proceed with the next part, something like this:
public void InputFieldEnter(string s){
if(s == ""){
errorMessage.SetActive(true);
}else{
// do whatever you want if the input is correct
}
}
In the previous case "errorMessage" is a GameObject type variable that has to be defined below the "Start" method and you need to make sure tu drag the GameObject of the error message in the field in the inspector, you define the variable like this:
public GameObject errorMessage; //Drag the error message in this field in the inspector
So, in summary, if you solve the problem this way, when you enters a null value in the input field, the message will appear, you click OK and enters the text again.
this only works if you have 1 input, what if you have multiple inputs and want to reuse the ReadStringInput functions? how does the code know which input to update?
Thanks so much. This was really helpful.
Is there a way to clear the input field after ?
yes, but you need to define the reference for an InputField component in your script, you can do it this defining a variable above the Start method for example:
public InputField theInputFieldIWantToClear;
If the prevous line gives you an error, prabably you need to import the proper namespace, above all add:
using UnityEngine.UI;
Then itˋs very important to initialize that variable with the component you want to clear, if you don´t do this step you´ll get a null reference exception. A simple way to initialize the variable is assigning the GameObject with the InputField component to the field called "theInputFieldIWantToClear" in the inspector of your script.
You have a video here:
th-cam.com/video/ESwadW9Ttzg/w-d-xo.html
Finally the clear part, for that you can do:
theInputFieldIWantToClear.text = "";
You can add the previous line were you need to clear the input field, for example in the OnEndEdit function that executes the InputField
Please subscribe to my other channel in english:
th-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.htmlvideos
@@GameDevTraum Great.
I have resubscribed.
Thanks again.
@@benjwano thanks!! 🙏🏽
Thanks ! Unity is my nightmare. Too complicated too learn.
Thank you Beyefendi
how can I limit characters in Input Field? For example: I want the name of a room given by a host player only within 20 characters
Dude great vid thanks... btw, why is bedroom voıice for :D
hahah xD I'll try to wake up earlier in the next video
btw I'm moving the new content in english to this other channel:
th-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.htmlvideos
does anybody know hwo to do the exact same thing but for doubles/floats/ints
Very helpful.!Thank you
is it possible to save the script in database(firebase)?
hum it appears to work, but my "input" var never updates, the message in the log is just blank.... hummm
Have you selected the dynamic string method in the on end edit function?
@@GameDevTraum Yup :(
I an manually assign the var a value and it shows. But it isn't based on the user input field. Tried some other things and same result...
hummm
edit:
imgur.com/a/egOn0or :)
Edit2:
I have also just tried to do debug.log(s); and same result... not have much luck reading these input fields
@@MoonLiteNite in the on end edit you have to look closely... there is a static area and a dynamic area.. both have the function in it. It seems you have selected the static one.
@@billyheinrich3279 Thank you ;)
@@billyheinrich3279 THIS OMG IM AN IDIOT THANK YOU!!
if it's for the answer, how can i make the input text becomes true or false?
is there a way i could have three or more input fields at once?
yes of course! mmm you can do it in different ways, for example, make each field execute it's own function inside the script (in the on end edit event of each input field), then add a button that executes another function that use the information entered.
This could be a code for that:
//Fields
string info1;//the info send by the input field 1
string info2;
string info3;
//Methods
//The next 3 functions are executed in the on end edit of each input field
//use methods listed in "dynamic strings"
public void SetInput1(string s){
info1=s;
}
public void SetInput2(string s){
info2=s;
}
public void SetInput3(string s){
info3=s;
}
public void ExecuteSomeFunction(){
//this function is executed by a button
//use info1, info2 and info3 to do whatever you want
}
Another alternative is perform a function when all the input fields aren't empty, for example:
public void SetInput1(string s){
info1=s;
if(info1!="" &&info2!="" &&info3!=""){
ExecuteSomeFunction();
}
}
public void SetInput2(string s){
info2=s;
if(info1!="" &&info2!="" &&info3!=""){
ExecuteSomeFunction();
}
}
public void SetInput3(string s){
info3=s;
if(info1!="" &&info2!="" &&info3!=""){
ExecuteSomeFunction();
}
}
public void ExecuteSomeFunction(){
//this function is executed by a button
//use info1, info2 and info3 to do whatever you want
}
@@GameDevTraum wooooow
how to make game to save this text and put it in this input field after restart game, tutorial from PrefixWiz doesnt work
heey, first let me tell you that I have anohter channel where I will be uploading the upcoming content in english (the one with the blue icon):
th-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.htmlvideos
Please consider subscribing to it.
About your problem, let's divide it in two parts. first saving and loading a string, you can find info about saving strings with player prefs here:
gamedevtraum.com/en/game-development/unity-tutorials-and-solutions/how-to-save-and-load-a-string-with-playerprefs-in-unity/
I suggest you create two functions, one to load and one to save, suppose you define a variable called "inputSavedData":
public string inputSavedData;
public void SaveData(){
PlayerPrefs.SetString(“aNameForYourData”,inputSavedData);
}
public void LoadData(){
inputSavedData = PlayerPrefs. GetString(“aNameForYourData”,””);
}
you can call LoadData in Start or Awake functions, like this:
void Start(){
LoadData();
}
And the saveData function you can call it where you read the input field, let's suppose you do that in a function called, InputFieldFunction:
public void InputFieldFunction(string inputText){
inputSavedData = inputText;
SaveData();
//do whatever you need
}
So up until here is the first part of the problem where you save the data and then recover it.
Now to write the saved text in the input field when loading, for that you'll need to define a reference for an InputField type component, first make sure you implement the UI namespace, above all the script add:
using UnityEngine.UI;
then above start you can define:
public InputField myInputField;
And then in Unity drag the InputField GameObject to the "myInputField" variable in the inspector of the script.
Once this is done you can write a text in the input field, so once you load the data, you can write the recover text to the input field:
void Start(){
LoadData();
myInputField.text = inputSavedData;
}
I hope you can solve your problem
bravo explained very well,
thnx
why do you put the main camera on the ui
Ohh that's because in this particular case the script that contains the function to be executed by the input field is assigned to the main camera
how do we take the text for put it somewhere
Is it working ? : input = textDisplay.GetCompenent().text;
The input variable is already the text, so you don't need to do anything else.
Thank you for your effort, that was very helpful. but what if I have two inputfields that I want to read and want to create a separate string for each inputfield? is that possible?
Yes of course, everything is possible :D
Just make two sepparate functions in your script and connect each input field to each function just how it's done in this video.
Inside the function you can make a boolean variable true indicating that the field was entered, and then check if the other input field is also entered and make things happen in that case. This could be an example:
//Here Begins C#
bool input1Ready=false;
bool input2Ready=false;
string input1Value;
string input2Value;
public void FunctionForInputField1(string textFromInput){
//Connect this function to one input field
input1Value=textFromInput;
input1Ready=true;
if(input1Ready && input2Ready){
//both inputs are entered, what should we do?
}
}
public void FunctionForInputField2(string textFromInput){
//Connect this function to another input field
input2Value=textFromInput;
input2Ready=true;
if(input1Ready && input2Ready){
//both inputs are entered, what should we do?
}
}
//Here Ends C#
btw dein Name klingt super Deutsch aus xD
Wow, the answer came quickly, thanks for that. And yes, the name is from a German children's book :)
Great video, thank you so much! I am trying to make a search tab in unity, where once I type something in the input field it checks it against my objects (courses in my case) and if there is a specific course with the name in my scene, then it will print it. How should I go about this?
EPIC. Many thanks!!!!!!
Great work!
How do I receive an integer input? Does it work if I just set content type in input field settings to "Integer Number" and then change the variable type in reading method to "int"?
I haven't try doing that, but you can parse the string value to int, I think the instruction is this:
int.Parse(stringValue);
I'll check in a couple hours
Yep, the InputField sends a string to the selected function inside the script, then you have to convert it to int. For example this way:
int intValue = int.Parse(stringValue);
intValue can also be defined as a global variable.
You also have the instruction:
int.TryParse(stringValue)
Which returns a boolean indicating weather the convertion was successful or not, in case the user writes text instead of numbers for example
@@GameDevTraum thanks! I did exactly the same thing but just before you answered me :)
@@GameDevTraum i have a question: for some reason when i try the parse thing that you did it resets my scene and i get errors saying the the value is not in the correct format. I would like to know what i did wrong. here is a copy of the section that gives errors: void Update()
{
if (enemiescount == null){
}
if (enemiescount1 == null){
}
if (enemiescount2 == null){
}
if (enemiescount3 == null){
}
if (enemiescount != null){
int enemycounter = int.Parse(enemiescount);
}
if (enemiescount1 != null){
int enemycounter1 = int.Parse(enemiescount1);
}
if (enemiescount2 != null){
int enemycounter2 = int.Parse(enemiescount2);
}
if (enemiescount3 != null){
int enemycounter3 = int.Parse(enemiescount3);
}
}
enemiescount(1-4) is the string that i am trying to convert into a integer so i can use that string to change a integer variable in another script as i am trying to make a sandbox mode for my game where i can type a number into an input field then that number will be the amount of enemies that spawn. i don't suppose you could help me with that?
¡Thanks, very nice!
very useful thank you
thanks dude!
¡Hi GameDevTraum!
When I finished watching your video, a question arose in me. Which functions the input field controls when an area of text is highlighted, for example to bold part of a dialog?
thank you!!
Thank you very much
Thanks a lot
amazing!
thank you
thanks!
Gracias!!!!!!!!!!!!!!!
Playback at .75
thank you very much