0

Upon a user registering the inputfield should autofill out the email address currently stored in user prefs. I have this working fine.

My problem is outputting this variable into the inputfield in the main page. Any help here would be great if anyone has managed it before. Every time I try to get the text of the inputfield it returns a null ref.

    GameObject inputFieldGo = GameObject.Find("email");
    InputField inputFieldCo = inputFieldGo.GetComponent<InputField>();
    inputFieldCo.text = "hello";

NullReferenceException: Object reference not set to an instance of an object firstRun.Start () (at Assets/Scripts/init/firstRun.cs:31)

Sean O'Donnell
  • 83
  • 2
  • 10
  • would that be `inputFieldCo.Text` in stead of `inputFieldCo.text` also what actually happens when you step through the code using the debugger..? – MethodMan Jun 27 '16 at 16:40
  • This error basically indicates that your `GameObject.Find()` method is returning a null and cannot find your specified element as [per the documentation](https://docs.unity3d.com/ScriptReference/GameObject.Find.html). You might consider posting the code for how you are defining it. Have you tried using `GameObject.FindWithTag()` instead? – Rion Williams Jun 27 '16 at 16:51
  • @methodman Thanks for the response. Everything works except the .text part. – Sean O'Donnell Jun 27 '16 at 16:55
  • @RionWilliams Hi, It finds the game object fine. I have it debugging to output when it finds it. When it looks for the text it returns Null. When I try to assign text it says null reference. – Sean O'Donnell Jun 27 '16 at 16:55
  • 1
    As mentioned in [this similar question](http://stackoverflow.com/a/28293066/557445), do you have a reference to `using UnityEngine.UI;` at the top? What does your `inputFieldCo` look like after the `GetComponent()` method is called? – Rion Williams Jun 27 '16 at 16:59
  • @RionWilliams Hi, Yes I am using that. I have a debug log of each command as it goes off. http://pixldrop.com/drop/BpiEFNG6hzCx59fs.png 1st is the game object being outputted, next is the inputfield being outputted, next is the text of the inputbox being outputted which is null because nothing is there? Then I try to set it and it responds with a nullref. – Sean O'Donnell Jun 27 '16 at 17:08
  • @RionWilliams All good got it working. Thanks! – Sean O'Donnell Jun 27 '16 at 17:13

2 Answers2

3

Checking Your Namespace

You'll need to ensure that you are explicitly including the appropriate Unity namespace for handling UI elements :

using Unity.UI;

Make Sure You Are Targeting the Proper Element

After that, you'll want to ensure that your GameObject.Find() method is targeting your InputField element properly (i.e. double-check to ensure the identifier is correct).

Make sure there are no typos (i.e. "email_text" instead of "email", etc.) as this will cause your method to return null and thus a Null Reference Exception when you attempt to set a property of it.

Make Use of the Debugger

If you are still running into issues, consider logging each step to determine where the null is being returned and then consider what might be wrong. Is something not being instantiated properly? Is there a naming error?

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
1

Got it working. Did the following below.

    GameObject email_go = GameObject.Find("email_input");
    Debug.Log(email_go);
    InputField email_in = email_go.GetComponent<InputField>();
    Debug.Log(email_in);
    Debug.Log(email_in.text);
    email_in.text = PlayerPrefs.GetString("email");
    Debug.Log(email_in.text);

Thanks to @RionWilliams

Sean O'Donnell
  • 83
  • 2
  • 10