- Home /
Duplicate Question
Text field issue switching from scene to another scene
Hi Guys,
i have created two scenes in my game. One is called "StartScene" and one "CreatePlayerScene". There is a text field in the last one which works perfectly if i start the scene directly from the editor. But if i switch from StartScene to CreatePlayerScene by pressing space the text field stop working and i get this error:
NullReferenceException: Object reference not set to an instance of an object UnityEngine.TextEditor.ClampPos () (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/TextEditor.cs:1251) UnityEngine.GUI.DoTextField (Rect position, Int32 id, UnityEngine.GUIContent content, Boolean multiline, Int32 maxLength, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUI.cs:550) UnityEngine.GUI.TextField (Rect position, System.String text) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUI.cs:440) MenuScript.OnGUI () (at Assets/Scripts/MenuScript.cs:41)
Why?
Here's the part of the code where i use the text field.
void OnGUI ()
{
GUI.skin = myGuiSkin;
if(!showTextField)
{
if(GUI.Button (new Rect(Screen.width/2f - Screen.width*0.1f, Screen.height/2f, 500, 100), "New Game"))
{
showTextField = true;
}
}
if(showTextField)
{
playerName = GUI.TextField(new Rect(Screen.width/2f - Screen.width*0.1f, Screen.height/2f, 500, 50), playerName);
PlayerPrefs.SetString("playerName", playerName);
}
}
There are around 700 previously asked questions about null reference exceptions.
Follow this Question
Related Questions
Change scene using text UI ? help 1 Answer
How do I reference something from another scene? 0 Answers
Getting Null Object Reference even though I'm not referencing an object 1 Answer
Big text field GUI. 1 Answer
Null Reference in Line 0 1 Answer