Reputation: 83
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)
Upvotes: 0
Views: 4612
Reputation: 76557
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?
Upvotes: 3
Reputation: 83
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
Upvotes: 1