Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trouble accessing InputField text

Discussion in 'UGUI & TextMesh Pro' started by digitoxin, Dec 27, 2014.

  1. digitoxin

    digitoxin

    Joined:
    Oct 9, 2013
    Posts:
    4
    Hi,

    I have scoured the unity forums and google trying to access an InputField text in C# and just cannot get it to work. I assume it is a simple error. These are the steps I have taken:

    1. GameObject > UI > InputField

    2. In a C# script attached to an empty object in the scene, I added this code

    Code (CSharp):
    1.  
    2. public class UIManagerScript : MonoBehaviour {
    3.         public GameObject initialsObject;
    4.         InputField initialsField;
    5.         Text initialsFieldInput;
    6.  
    7.         public void Start(){
    8.              initialsField = initialsObject.GetComponent<InputField>();
    9.              initialsFieldInput = initialsField.GetComponent<Text>();
    10.        }
    11.  
    12.       public void otherFunction(){
    13.             Debug.Log(initialsFieldInput.text);
    14.             // error: Object reference not set to an instance of an object
    15.       }
    16. }
    3. dragged the InputField object created in #1 from hierarchy to "Initials Object" in script inspector

    As I understand it, when I create the InputField from the menu in #1, a GameObject is made with the InputField script attached. Is this correct?

    Any help is appreciated! Thanks!
     
  2. Raimis

    Raimis

    Joined:
    Aug 27, 2014
    Posts:
    160
    No need to access via Text component. Text component is exposed via InputField.textComponent and value of that text is accessible via InputField.text. Hope this helps
     
    digitoxin likes this.
  3. digitoxin

    digitoxin

    Joined:
    Oct 9, 2013
    Posts:
    4
    solved! thank you!
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    just a side note (even though your issue has been solved)

    the Text component text value is not guaranteed to be the same as the InputField text value. This is due to the inputfield modifying the string before using the Text component to display the value.
     
    Sincoyw likes this.
  5. digitoxin

    digitoxin

    Joined:
    Oct 9, 2013
    Posts:
    4
    i see. so to get the Text component's text value directly, what am I doing wrong (from the code at the top)?

    thanks!
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    initialsFieldInput = initialsField.GetComponent<Text>(); isn't needed for one. you can just do initialsField.textComponent (if you still want the Text component.

    initialsField.text is all you need to get the full value of the input fields text.
     
    multimediamarkers and digitoxin like this.
  7. Sincoyw

    Sincoyw

    Joined:
    Sep 22, 2015
    Posts:
    10

    but now i have another problem, if i use a Chinese Input Method, such as Sogo, if the IME has characters before the InputField Initialized, then the InputField.text will always get the characters before the value you set. How can I do?