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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Player name from Input Field?

Discussion in 'Scripting' started by groch, May 13, 2015.

  1. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Hi there,

    How can I get the text from Input Field? I want it to be equal to "this.playerName":

     
  2. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    You need to access the text from the Text component of the gameObject called Text that is the child of the input field. That's a lot of different types of text, let me show you example of some code to get the information from an Input Field.

    Code (csharp):
    1.  
    2. Text userNameInputText;
    3.  
    4. void Start() {
    5.   userNameInputText = canvas.transform.Find("UserNameInputField/Text").GetComponent<Text>(); //You'll need to put in the actual path in your own hierarchy.
    6. }
    7.  
    8. public void InputYourName() {
    9.   playername = userNameInputText.text;
    10. }
    11.  
     
    groch likes this.
  3. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Pretty sure you can just do InputField.text
     
    Tentakero and groch like this.
  4. groch

    groch

    Joined:
    Apr 7, 2015
    Posts:
    29
    Unfortunately this didn't worked :(

    Thank you for your effort, I tried it and error came up:

    "The name 'canvas' does not exist in the current context"

    How can I fix that?

    Also, in place "UserNameInputField/Text" I have to put my own path to the Input Field's Text? In my case it looks like that:



    So how it should look like in the script?

    EDIT:

    Ok, now it's working, I have added

    Code (CSharp):
    1. public GameObject canvas;
    then added Canvas 1 (from hierarchy) to the proper place in Inspector and corrected my path:

    Code (CSharp):
    1. userNameInputText = canvas.transform.Find("InputField/Text").GetComponent<Text>();
    Thanks! :)
     
    Last edited: May 13, 2015