Search Unity

How would I get the text input from a text field into a variable?

Discussion in 'Scripting' started by MattCarter24, Jun 1, 2015.

  1. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I am a beginner with no epxerience really so please make your explanations imsple and easy to understand :).

    I would like a script to run on the click of a button, to grab the text input from a text field, store the input in a variable and then use it as the condition in an if statement.

    Please also demonstarate how I would link up the script with the button or any other links I must establish.

    Thanks - Any help would be much appreciated :D PS: I don't mind if your answer is in UnityScript or C#.

    I bet this is horribly wrong but I'd assume the code would look something like this.

    var x = InputField.text;
    if (var x = "Hello"){
    var score = score + 1;
    }

    I don't know, this is clearly my web design Javascipt mode coming out of me even though it is way different for Unity.
     
    lenome and chrisk8er like this.
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Code (CSharp):
    1. InputField iField;
    2. string myName;
    3.  
    4. void MyFunction()
    5. {
    6.       Debug.Log(iField.text);
    7.       myName = iField.text;
    8.      
    9.       if (myName == "MattCarter")
    10.       {
    11.             Debug.Log("Welcome back Mr. Carter");
    12.       }
    13.       else
    14.       {
    15.            Debug.Log("I Don't Know You!");
    16.       }
    17. }
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I've got a similar set up in my ChatBox video. From about 14 minutes onwards.



    I basically wire up a method to the InputField's OnChange event, then the button can also reference the stored data.
     
    TheGoldenEgg2020 and NomadKing like this.
  5. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    @5vStudios hello, thank you very much for your help! It doesn't work though. There is an error. Apparently the name InputField does not exist in the current context?
     
  6. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    You have to add the using statement:
    Code (CSHARP):
    1. using UnityEngine.UI;
    at the top of your c# script, unity 5
     
  7. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Wait I think it is working? Please wait and I will inform you of the situation in a minute.
     
  8. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    How do I check to see if this debug.log is working? How can I make a pop up in the Android app itself?
     
  9. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Why does y code not work? Here it is...

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class CheckAnsL1 : MonoBehaviour {

    InputField iField;
    string myName;

    void MyFunction()
    {
    Debug.Log(iField.text);
    myName = iField.text;

    if (myName == "MattCarter")
    {
    Debug.Log("Correct!", "The word 'cityi is correct!", "Ok");
    }
    else
    {
    Debug.Log("Incorrect!", "The answer was 'city'.", "Ok");
    }
    }
    }
     
  10. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    not working ? Please be more specific
     
    MattCarter24 likes this.
  11. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Code (CSHARP):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class CheckAnsL1 : MonoBehaviour
    6. {
    7. public InputField iField;
    8. public string myName;
    9. public string myText;
    10.  
    11. void MyFunction()
    12. {
    13. Debug.Log(iField.text);
    14. myName = iField.text;
    15.  
    16. if (myName == "MattCarter")
    17. {
    18. Debug.Log("Correct! The word " + myText + " cityi is correct! Ok");
    19. }
    20. else
    21. {
    22. Debug.Log("Incorrect! The answer was " + myText + " Ok");
    23. }
    24. }
    25. }
     
    Last edited: Jun 1, 2015
    MattCarter24 likes this.
  12. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    upload_2015-6-1_22-38-19.png

    It says this. When I click on the button to run the script nothing happens. I can't see if it is working or not?
     
  13. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    If the input field contains the correct word, I want it to add a point to a scoring system but I haven't been able to work out who to create a scoring system regardless of all the tutorials I have watched. I don't even know how to get the score to display in a text box either and yes I have watched the tutorials on Unity but my application is different as the scripts in the tutorial apply to scenes with completely different coding to mine and I just can't seem to get anything to work for me?
     
  14. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    I also renamed the text input field to iField. I hope that is also correct.
     
  15. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    MattCarter24 likes this.
  16. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello, I added the script to an empty gameobject and linked it to the button. I don't know what to choose under the menu that you can see in this image?
     
  17. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    The image should be attached to this message
     

    Attached Files:

  18. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    ah ok, first off you have to modify the script and change
    Code (CSHARP):
    1. void MyFunction()
    2. to
    3. public void MyFunction()
    then 'MyFunction' should appear in that list that you posted above
     
    MattCarter24 likes this.
  19. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    is this normal even if i entered in the correct answer?
    upload_2015-6-1_23-24-59.png
     
  20. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    that error means that you did not add the reference to the object in he inspector of the script
     
    MattCarter24 likes this.
  21. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    click on the gameobject that you added the 'CheckAnsL1' script to then look in the inspector and added the InputField GameObject (Under your UI canvas) to the iField field in the inspector
     
    MattCarter24 likes this.
  22. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Sorry I don't quite understand your last post.

    "click on the gameobject that you added the 'CheckAnsL1' script to then look in the inspector and added the InputField GameObject (Under your UI canvas) to the iField field in the inspector"

    Sorry I'm confused.
     
  23. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Are you saying something about this?

    upload_2015-6-1_23-48-24.png
     
  24. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    yes i am, in the script you need to change
    Code (CSHARP):
    1. InputField iField;
    2. to
    3. public InputField iField
    then it will show up in the window under 'CheckAnsL1' in the picture that you posted above
     
    brenospore and MattCarter24 like this.
  25. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    THANK YOU SO MUCH IT WORKS! Can I ask you another favour please. How would I create a very simple score function so if I got entered the correct word, it would add 1 point to the score. I would like the score to be displayed at the bottom of the screen across multiple scenes. Could you please show me the code necessary because I promise you I watched the tutorials on the Unity website but they did not work. Please help.
     
  26. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Yeah sure, just a second, I'm on my LG G3.

    be sure to like my post if my assistance helped you, it helps me
     
  27. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    I would love to help you since you have saved my life I can't thank you enough!
     
  28. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    I just liked every post that you uploaded in this entire forum! :)
     
  29. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello? Sorry just wanted to let you know I may not see your response if it isn't within the next cocuple of hours as I will need to go to bed :)
     
  30. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Sorry to be a pain but could I please remind you to reply if you have forgotten about me :) ?
    THANK YOU SO MUCH IT WORKS! Can I ask you another favour please. How would I create a very simple score function so if I got entered the correct word, it would add 1 point to the score. I would like the score to be displayed at the bottom of the screen across multiple scenes. Could you please show me the code necessary because I promise you I watched the tutorials on the Unity website but they did not work. Please help.
     
  31. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Code (CSHARP):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class CheckAnsL1 : MonoBehaviour
    6. {
    7.        public InputField iField;
    8.        public string myAns;
    9.  
    10.        public int myScore;
    11.  
    12.        void Score()
    13.        {
    14.               myAns = iField.text;            
    15.  
    16.               if (myAns == "Florida")
    17.               {
    18.                      Debug.Log("Correct Answer");
    19.                      myScore = myScore + 1;
    20.               }
    21.               else
    22.               {
    23.                      Debug.Log("Incorrect");
    24.               }
    25.        }
    26. }
     
  32. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Thank you I have not had the chance to try it out yet but thank you for replying! You have been so helpful! :)
     
  33. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Would the code you uploaded above be inserted in to the CheckAnsL1 script? Or a new one?
    Also, would it be pasted below the current code in the CheckAnsL1 or completely replace everything that is already there?

    Also, do you know how to get that score variable into a UI text in multiple scenes. Since it is in multiple scenes does that involve the need to change the code so that it keeps the current socre across all the scenes that are swapped to. Such as a DontDestroyOnLoad? I don't know where I would even put it?
     
    Last edited: Jun 3, 2015
  34. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello @5vStudios , I tried putting the code above in a new script and the already exisiting script and it didn't work. Please make sure that this script is able to carry the score over to another scene and that in the different scenes with this code, I'd like to enter a different correct word for each scene, but still have the script be able to add a point to the score.

    How would my score be placed into UI text? How do I link them together?

    Please help
     
  35. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello @5vStudios sorry to be a pain but could you please reply? Just reminding in case you have forgotten about me :/ PS: I'm really paranoid, sorry. :)
     
  36. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Good lord man - stop repeat posting. This forum is for assistance with coding-related issues and not for requesting complete scripts. If you've attempted something and it's not working then please post a new thread with relevant code snippets and a detailed explanation of what is happening and what errors you're getting.
     
  37. shete

    shete

    Joined:
    Oct 20, 2015
    Posts:
    1
    Sir I want two text box .....and one button.. And on click event of text box text in text box stored inside the variable...so how I do it
     
  38. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    here is the easy way to get input filed value on click
     
  39. Boomboyag

    Boomboyag

    Joined:
    Oct 13, 2020
    Posts:
    33
    Is there a way to look for certain keywords inside the input field? I am making a text - based game and didn’t want to restrict the player to a few specific answers. Anyone know how I could go about this?
     
  40. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  41. Boomboyag

    Boomboyag

    Joined:
    Oct 13, 2020
    Posts:
    33
    Thanks!
     
  42. panendraommina

    panendraommina

    Joined:
    Aug 22, 2019
    Posts:
    2
    thank you so much .
     
  43. Escoce

    Escoce

    Joined:
    Dec 9, 2020
    Posts:
    5
    I have to be honest, until I got to message #24 on this thread I couldn't figure this out either. The documents even 7 years later really should be updated to include this step.
     
  44. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  45. deryldev

    deryldev

    Joined:
    Nov 25, 2022
    Posts:
    6
    thanks :)