0

Solution
The issue is the difference between Text Mesh Pro InputField and the native InputField. I was using TMP which seems to be different when referencing it. I reverted to native Inputfield and it worked immediately.

Original Error
I am trying to save the text from an inputfield into a variable to use across scenes. Currently I cant even get it to save the text alone as I get a null-reference. Ive tried multiple solutions and nothing is working. Using the exact same examples from YouTube and other online sources.

I have also tried using PlayerPrefs.SetString / GetString with no real luck.

Current Setup is as follows.

  1. Script below collects and saves the text to a public variable. Script is attached to empty GameObject
  2. InputText is associated to the text part of the inputField When pressing Start I get the null reference.
  3. Start button simply calls the "SaveAddress()" Function

Error Message

NullReferenceException: Object reference not set to an instance of an object
SaveTipBotAddress.SaveAddress () (at Assets/Scripts/SaveTipBotAddress.cs:25)

Text Scripts

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

public class SaveTipBotAddress : MonoBehaviour
{

    public string ilpAddress;
    public GameObject inputText;

    public void SaveAddress()

    {

        ilpAddress = inputText.GetComponent<Text>().text;
    }

}

InputField Text referenced in Script

Start Button Calls SaveAddress Script

snub-fighter
  • 161
  • 2
  • 14
  • Your `inputText` field is set to `ILP_TextField` but you did not show that object. – Draco18s no longer trusts SE Sep 27 '19 at 21:23
  • You probably referenced the wrong object - not the one which the `Text` component attached but the one with the text area component. In that case `GetComponent` doesn't find the `Text` and returns `null`. To be sure next time you can use `GetComponentInChildren(true)` Instead or have a look at [Ebleme's answer](https://stackoverflow.com/a/58141660/7111561) – derHugo Sep 28 '19 at 04:14
  • It shows in the images. ILP_TextField is the actual text inside the InputField so knowing its referenced should be sufficient. however that being said it was a TMP InputField issue. Refer to edit on original Q and answer below. – snub-fighter Sep 28 '19 at 18:59

2 Answers2

0

I see that ILP_TextField is a Text object inside an InputField. In order to get text from an InputField you need to get a reference of that InputField after than you can get your text using InputField.text property.

On the Inspector you nedd to set assing the "Tex Area" object to Input Text Area field.

Your script will be like that:

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

public class SaveTipBotAddress : MonoBehaviour
{

    public string ilpAddress;
    //public GameObject inputText;

    public InputField inputTextArea;

    public void SaveAddress()    
    {
        ilpAddress = inputTextArea.text;
    }    
}
Ebleme
  • 287
  • 2
  • 13
0

Well I figured out the issue. All answers were helpful but the main issue was I was using InputField - TextMeshPro. Im not sure on the difference between calling fields in that and the native InputField but it worked right away when I used the native InputField.

Sorry and Thank you.

snub-fighter
  • 161
  • 2
  • 14