Text Input Error.

Hello everyone. Im still a newb on using unity. :roll_eyes::roll_eyes:

So the problem I present today is this: I ask the PlayerPrefs if the user is a first timer, if so ask him his name.
What I don’t really know how to solve this is the next.

I’ve created a couple of scripts but I don’t know what to attach them too. If I want to load up first before main menu then my assumption would be:

Creating an empty scene just to load everything up and create a script called loadup, which loads up user’s profile settings. If its the user’s first time then continue to main menu. So, I thought I had it right but here’s what happens. I added a script called loadup and attached it to the MainCamera with this:

LoadStartup.cs

public class LoadStartup : MonoBehaviour {

//#pragma strict


	private int PlayedBefore;
	private string textInput;
	private string textInputHeader;

	void Awake () 
	{

			PlayedBefore = PlayerPrefs.GetInt("FirstTimer");
			Debug.Log("Awaking, so PlayedBefore = " + PlayedBefore);
			
			// Checks if this is the first time you play
			
			if(PlayedBefore == 0) {

				//First Time playing, so we set 'PlayeBefore to 'True'
			Debug.Log ("First Timer");
				/*PlayedBefore = 1;
				PlayerPrefs.SetInt("FirstTimer", (PlayedBefore)); */
				
				
				// Checking the values
				
				Debug.Log("PlayedBefore = " + PlayedBefore);
				
				
			} else {
				// Else we already played, continue to next scene or something.

				Debug.Log ("You've Already Played");
				Debug.Log("PlayedBefore = " + PlayedBefore);
			}
			
		}

All goes well until here. it does check if the user is a first timer.

But here, is the real deal.

And where my question goes a long way. as OnGUI is called constantly, I wouldn’t seem fit to add the inputbox here but I had a compiler error saying “You can only call GUI functions on a OnGUI method” or something like that.

And furthermore, I get an error on the line where it says" textInput = GUI.TextInput…"

Here’s the error: NullReferenceException: Object not sent to an instance of an object"

Here’s the OnGUI method

	/* Here goes onGui things. Test ask user name */
	void OnGUI ()
	{

		if (PlayedBefore == 0) 
		{
			Debug.Log ("First timer, Adding Questions");
			
			textInputHeader = "Enter your Name:";
			
			textInput = GUI.TextField (new Rect (Screen.width * 0.5f, Screen.height * 0.85f, Screen.width * 0.25f, Screen.height * 0.1f),textInput, 25);
		} else if (PlayedBefore == 1) 
			{
			GUI.Label (new Rect(Screen.width * 0.5f, Screen.height * 0.85f, Screen.width * 0.25f, Screen.height * 0.1f), "Welcome "+ textInput);
			}


	}
}

Thanks for your time reviewing this question. If you could explain to me why its happening(error) and what would be your personal solution to my objective.(which would be adding a small box asking the user its name.(Instead of using OnGUI)

Have an awesome day! Just like you people are! :smile:

Try adding textInput =""
in the awake function

So, it did work! Thanks a bunch…but why???

As for my second problem, do you think its best practice to ask for user input on the “OnGUI” method???(as it constantly updates).

Thanks a bunch for your time man!