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

Question Trying to pass input field text into string variable

Discussion in 'Scripting' started by DarthHawk13, Feb 2, 2022.

  1. DarthHawk13

    DarthHawk13

    Joined:
    Feb 24, 2016
    Posts:
    63
    The variable name 'playerFactionName' refers to an input field on the UI.
    I have a button on the UI the player clicks to move onto the next section of setting up the game. When that button is clicked the text entered by the user in the input field is supposed to be passed to a string variable.


    I've tried the following different single lines of code and each time I get an error.

    (string variable)= playerFactionName.text;
    (string variable)= playerFactionName.text.ToString();
    (string variable)= playerFactionName.textComponent.text;
    (string variable)= playerFactionName.textComponent.text.ToString();

    every time I get the following error:
    "Object reference not set to an instance of an object" and it refers to the line of code I'm having issues with. The playerFactionName variable does correctly refer to the input field on the UI. It's a public variable and I dragged the input field to it in the inspector. The string variable I want to store the information does exist in the script and is properly declared.

    Maybe the solution is soooo simple and I just need to take a break for a while before I get back to this but it's driving me nuts! I don't understand why it's not working.

    On a different UI in the same game I have another input field which the user can enter numbers (a seed) into for generating terrain. A script is executed as soon as the input field text is finished being edited by the user. The following code works for it perfectly and the terrain is generated as expected.

    theSeed is the variable name of the input field. This input field is setup in the inspector so only numbers can be entered by the user which is why I don't bother checking to make sure the user only entered numbers.

    (int variable) = Convert.ToInt32(theSeed.text);
    There are no problems with this line of code.

    I don't understand why my first example isn't working and the second one does. Shouldn't the following code work?
    (string variable)= playerFactionName.text;

    Is it not working because I'm having a script pull the text info from the input field text instead of having a script executed directly by the input field UI element when it detects the text has been changed?
     
    Last edited: Feb 2, 2022
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    The answer is always the same!
    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    If you have the line it's pointing to, then you can check what could possibly be null. Then your next step is to find out why it's null! Even if you think it should be working correctly, you need to start debugging it.

    playerFactionName.text is the correct way to get a value from an inputfield, so if that line is null, then you need to figure out why playerFactionName is null. Debug.Log it, check your reference during, before, and after your code runs. Make sure you don't have multiple copies of a script and are pointing to the wrong one. There are various reasons why it could be null even when you think it shouldn't.
     
    Kurt-Dekker likes this.
  3. DarthHawk13

    DarthHawk13

    Joined:
    Feb 24, 2016
    Posts:
    63
    I came back to working on it about 10 minutes ago. There is now no problem. I wonder it exiting out of Unity and loading up the project again fixed it? I read the link you posted and setup a few debug log commands in the script anyway. They all show it is working. That's weird it would do that in the first place. Is there a way for me to check if my copy of visual studio is missing anything without uninstalling and reinstalling it?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    If you run the VS installer again, I believe it offers a repair option if you're worried about it. Note that if you installed it with the Unity installer through the hub, you'll have to download the VS installer on it's own and then run that.