{ How to get text from INPUT FIELD in Unity } - User Text Entries

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 99

  • @GameDevTraum
    @GameDevTraum  3 ปีที่แล้ว +3

    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

  • @greendragonpng9068
    @greendragonpng9068 2 ปีที่แล้ว +63

    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).

    • @wmetz1869
      @wmetz1869 2 ปีที่แล้ว +2

      Yea this is easy to miss.

    • @neilfosteronly
      @neilfosteronly 2 ปีที่แล้ว +4

      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

    • @GameDevTraum
      @GameDevTraum  2 ปีที่แล้ว +1

      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

    • @sigge.g2193
      @sigge.g2193 ปีที่แล้ว +1

      Thanks you bro!

    • @Oxideacid
      @Oxideacid ปีที่แล้ว +1

      Youre a lifesaver! Thank you!

  • @ano3661
    @ano3661 2 ปีที่แล้ว +1

    Thanks, finally. I searched for a lot long useless things but finally found that

  • @turkeyjerkey
    @turkeyjerkey 3 ปีที่แล้ว +5

    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!

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว

      Great!! I'm glad that helped 😄😄

  • @rogpigamedev
    @rogpigamedev ปีที่แล้ว +1

    DUDE, LOVE YOU, FAST AND TO THE POINT, FINALLY. THANKS

  • @AlexiHusky
    @AlexiHusky 3 ปีที่แล้ว +8

    Fast and to the point 👌 gotta love a well rehearsed and spoken video

  • @politbureau5407
    @politbureau5407 2 ปีที่แล้ว

    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!

  • @saridos1000
    @saridos1000 2 ปีที่แล้ว +1

    how would i use their text in a if statement?

  • @edajb
    @edajb 3 ปีที่แล้ว +1

    Very well explained and under 4 min!

  • @chromaticilluminatic9808
    @chromaticilluminatic9808 3 ปีที่แล้ว +3

    Thank you so much! Short and clear.

  • @eu6250
    @eu6250 2 ปีที่แล้ว +1

    you are a god amongst men my friend

  • @codewithcola8424
    @codewithcola8424 2 ปีที่แล้ว +2

    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)

    • @iromanstr
      @iromanstr หลายเดือนก่อน

      thank u

  • @druwayu
    @druwayu ปีที่แล้ว +1

    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.

  • @ПавелВорожцов-т5э
    @ПавелВорожцов-т5э 2 ปีที่แล้ว +1

    Thats exactly what i needed thank you!

  • @xsomili5501
    @xsomili5501 3 ปีที่แล้ว +2

    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

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว +2

      Heey, make sure you choose the dynamic string function in the the drop-down menu of the on end edit event

    • @xsomili5501
      @xsomili5501 3 ปีที่แล้ว +1

      @@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

  • @elainechen5033
    @elainechen5033 3 ปีที่แล้ว

    Thank you so much for making this tutorial!!

  • @dogukanalkan8647
    @dogukanalkan8647 3 ปีที่แล้ว

    Thank you for simple and instructive video.

  • @lorenzobergamini4657
    @lorenzobergamini4657 3 ปีที่แล้ว +3

    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?

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว +2

      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
      @lorenzobergamini4657 3 ปีที่แล้ว

      @@GameDevTraum How can I select the dynamic function? I can't see any setting to change it

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว +11

      @@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

    • @lorenzobergamini4657
      @lorenzobergamini4657 3 ปีที่แล้ว +3

      @@GameDevTraum Oh I didn't even notice that, thank you very much!

  • @SnowB34R
    @SnowB34R 2 ปีที่แล้ว

    Quick and easy to understand! Thanks!

  • @sbellor89
    @sbellor89 ปีที่แล้ว

    How do I validate the input field? More specifically if it's an empty field (null) prompt the player to input text again?

    • @GameDevTraum
      @GameDevTraum  ปีที่แล้ว

      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.

  • @Rollbaa
    @Rollbaa ปีที่แล้ว

    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?

  • @benjwano
    @benjwano 2 ปีที่แล้ว +2

    Thanks so much. This was really helpful.
    Is there a way to clear the input field after ?

    • @GameDevTraum
      @GameDevTraum  2 ปีที่แล้ว +2

      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

    • @GameDevTraum
      @GameDevTraum  2 ปีที่แล้ว +1

      Please subscribe to my other channel in english:
      th-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.htmlvideos

    • @benjwano
      @benjwano 2 ปีที่แล้ว +1

      @@GameDevTraum Great.
      I have resubscribed.
      Thanks again.

    • @GameDevTraum
      @GameDevTraum  2 ปีที่แล้ว

      @@benjwano thanks!! 🙏🏽

  • @dragonbleu1205
    @dragonbleu1205 7 หลายเดือนก่อน

    Thanks ! Unity is my nightmare. Too complicated too learn.

  • @nonamenosname5300
    @nonamenosname5300 3 ปีที่แล้ว +1

    Thank you Beyefendi

  • @TienPham-kl7tf
    @TienPham-kl7tf ปีที่แล้ว

    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

  • @stratocasting
    @stratocasting 2 ปีที่แล้ว +1

    Dude great vid thanks... btw, why is bedroom voıice for :D

    • @GameDevTraum
      @GameDevTraum  2 ปีที่แล้ว

      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

  • @vievienu-2903
    @vievienu-2903 ปีที่แล้ว

    does anybody know hwo to do the exact same thing but for doubles/floats/ints

  • @vincentperon8645
    @vincentperon8645 2 ปีที่แล้ว

    Very helpful.!Thank you

  • @shingloong6994
    @shingloong6994 3 ปีที่แล้ว +1

    is it possible to save the script in database(firebase)?

  • @MoonLiteNite
    @MoonLiteNite 3 ปีที่แล้ว +1

    hum it appears to work, but my "input" var never updates, the message in the log is just blank.... hummm

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว

      Have you selected the dynamic string method in the on end edit function?

    • @MoonLiteNite
      @MoonLiteNite 3 ปีที่แล้ว

      @@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

    • @billyheinrich3279
      @billyheinrich3279 3 ปีที่แล้ว

      @@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.

    • @KeithAdam
      @KeithAdam 3 ปีที่แล้ว

      @@billyheinrich3279 Thank you ;)

    • @bbg6534
      @bbg6534 2 ปีที่แล้ว

      @@billyheinrich3279 THIS OMG IM AN IDIOT THANK YOU!!

  • @AsmaraJo
    @AsmaraJo 3 ปีที่แล้ว

    if it's for the answer, how can i make the input text becomes true or false?

  • @fieriko5808
    @fieriko5808 3 ปีที่แล้ว +2

    is there a way i could have three or more input fields at once?

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว +2

      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
      }

    • @Mikelica69
      @Mikelica69 3 ปีที่แล้ว +1

      @@GameDevTraum wooooow

  • @llYuki0okami
    @llYuki0okami 2 ปีที่แล้ว

    how to make game to save this text and put it in this input field after restart game, tutorial from PrefixWiz doesnt work

    • @GameDevTraum
      @GameDevTraum  2 ปีที่แล้ว

      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

  • @harshjadon1662
    @harshjadon1662 ปีที่แล้ว

    bravo explained very well,
    thnx

  • @unamed_user902
    @unamed_user902 3 ปีที่แล้ว +1

    why do you put the main camera on the ui

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว +1

      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

  • @xB2_
    @xB2_ 3 ปีที่แล้ว

    how do we take the text for put it somewhere
    Is it working ? : input = textDisplay.GetCompenent().text;

    • @shadesoftime
      @shadesoftime 3 ปีที่แล้ว

      The input variable is already the text, so you don't need to do anything else.

  • @gamsensvongamenetspringgeh6273
    @gamsensvongamenetspringgeh6273 3 ปีที่แล้ว +1

    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?

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว +2

      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

    • @gamsensvongamenetspringgeh6273
      @gamsensvongamenetspringgeh6273 3 ปีที่แล้ว

      Wow, the answer came quickly, thanks for that. And yes, the name is from a German children's book :)

  • @aanandisidharth9975
    @aanandisidharth9975 3 ปีที่แล้ว

    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?

  • @Splarkszter
    @Splarkszter 2 ปีที่แล้ว

    EPIC. Many thanks!!!!!!

  • @starboy_2408
    @starboy_2408 3 ปีที่แล้ว

    Great work!

  • @shadesoftime
    @shadesoftime 3 ปีที่แล้ว +1

    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"?

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว

      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

    • @GameDevTraum
      @GameDevTraum  3 ปีที่แล้ว

      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

    • @shadesoftime
      @shadesoftime 3 ปีที่แล้ว +1

      @@GameDevTraum thanks! I did exactly the same thing but just before you answered me :)

    • @JJsHowToChannel
      @JJsHowToChannel 2 ปีที่แล้ว

      @@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?

  • @davidSMarquez
    @davidSMarquez 2 ปีที่แล้ว

    ¡Thanks, very nice!

  • @RifinTidHamar
    @RifinTidHamar 2 ปีที่แล้ว

    very useful thank you

  • @rafaelmoura4714
    @rafaelmoura4714 10 หลายเดือนก่อน

    thanks dude!

  • @zonagamer9566
    @zonagamer9566 2 ปีที่แล้ว

    ¡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?

  • @tzipy7580
    @tzipy7580 2 ปีที่แล้ว +1

    thank you!!

  • @lofiman1254
    @lofiman1254 3 ปีที่แล้ว

    Thank you very much

  • @НатальяДмитриева-г3с
    @НатальяДмитриева-г3с ปีที่แล้ว

    Thanks a lot

  • @askardw636
    @askardw636 ปีที่แล้ว

    amazing!

  • @DasCas
    @DasCas ปีที่แล้ว

    thank you

  • @tOBViJprCUK
    @tOBViJprCUK 3 ปีที่แล้ว +1

    thanks!

  • @Mikelica69
    @Mikelica69 3 ปีที่แล้ว +1

    Gracias!!!!!!!!!!!!!!!

  • @dericortiz7382
    @dericortiz7382 3 ปีที่แล้ว

    Playback at .75

  • @holyone2460
    @holyone2460 ปีที่แล้ว

    thank you very much