- Home /
GUILayout.TextField doesn't accept inputs
Well, i'm pretty sure this is a common problem caused for insufficient knowledge, i tried veryfing other questions and threads but i don't get them at all because they all have always something more specific as, for example, making a button dissapear or showing again depending on a certain state and some other things like that, but my problem is more simple i think.
I'm trying to learn networking since i want to make my game multiplayer, and, like in survivors, have a dialog to start the game as the server and another dialog to connect to another server, in which i want to have a Text Field to write the IP Address into. I managed to get my preferred layout look, but, i can't type anything into the TextField, i read in other questions that i must make sure i'm not calling the content inside the textfield from any other function, so i only called it from function Start() to set it to a default value ("192.168.1.100", only to test the connection, because this is my private IP at the moment), and i also tried to delete this call from Start() so it wouldn't got changed never, but Unity says:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.TextEditor.ClampPos () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/Export/TextEditor.cs:1185)
UnityEngine.GUI.DoTextField (Rect position, Int32 id, UnityEngine.GUIContent content, Boolean multiline, Int32 maxLength, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/GUI.cs:496)
UnityEngine.GUILayout.DoTextField (System.String text, Int32 maxLength, Boolean multiline, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/GUILayout.cs:96)
UnityEngine.GUILayout.TextField (System.String text, UnityEngine.GUILayoutOption[] options) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/GUILayout.cs:57)
Menu Principal.OnGUI () (at Assets/Mis Scripts/Menu Principal.js:225)
That would mean that anytime i want to use a TextField, i can't leave it 'empty', isn't it?
I'll leave to you the hole code of my Main Menu (some parts are in Spanish, sorry about that):
#pragma strict
private var mostrarMenuPrincipal : boolean = true;
private var mostrarOpciones : boolean = false;
private var mostrarBotonOpciones : boolean = true;
private var mostrarBotonVolverMenuPrincipal : boolean = false;
private var mostrarMenuUnJugador : boolean = false;
private var mostrarBotonCancelar : boolean = false;
private var mostrarMenuMultijugador : boolean = false;
private var mostrarMenuModo1Multijugador : boolean = false;
private var mostrarDialogoServer : boolean = false;
private var mostrarDialogoHuesped : boolean = false;
private var InputCustomIP : String;
private var toggleDificultad1 : boolean = false;
private var toggleDificultad2 : boolean = false;
private var toggleDificultad3 : boolean = false;
var supportedNetworkLevels : String[] = [ "mylevel" ];
var disconnectedLevel : String = "loader";
private var lastLevelPrefix = 0;
//private var estadoActualPantalla : boolean;
enum NetworkConnectionError
{
TooManyConnectedPlayers,
}
function Start()
{
mostrarMenuPrincipal = true;
Time.timeScale = 1;
InputCustomIP = "192.168.1.100";
}
function Awake ()
{
// Network level loading is done in a separate channel.
networkView.group = 1;
}
function Update()
{
}
function OnGUI ()
{
//MENU PRINCIPAL
if(mostrarMenuPrincipal)
{
// Make a background box
GUI.Box (Rect ((Screen.width * 0.5)-75,(Screen.height * 0.5)-75,170,134), "Menu Principal");
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect ((Screen.width * 0.5)-65,(Screen.height * 0.5)-50,150,23), "Un Jugador"))
{
mostrarMenuPrincipal = false;
mostrarMenuUnJugador = true;
mostrarBotonCancelar = true;
mostrarBotonOpciones = false;
}
if (GUI.Button (Rect ((Screen.width * 0.5)-65,(Screen.height * 0.5)-25,150,23), "MultiJugador"))
{
mostrarMenuPrincipal = false;
mostrarMenuUnJugador = false;
mostrarBotonCancelar = true;
mostrarBotonOpciones = false;
mostrarMenuModo1Multijugador = true;
mostrarBotonVolverMenuPrincipal = true;
Application.runInBackground = true;
}
if (GUI.Button (Rect ((Screen.width * 0.5)-65,(Screen.height * 0.5)+25,150,23), "Salir"))
{
Application.Quit();
}
}
//MENU PARTIDA
if(mostrarMenuUnJugador)
{
//GUI.Box (Rect (50,50,(Screen.width -100),(Screen.height -150)), "Ajustes Partida");
GUILayout.BeginArea(Rect ((Screen.width * 0.5)-225,(Screen.height * 0.5)-75,450,130));
GUI.Box (Rect (0,0,450,125), "Ajustes Partida");
GUILayout.BeginArea(Rect (5,20,440,200));
//GUILayout.Box("Nota: Por ahora..., estos controles de dificultad son solo de relleno.");
/*GUILayout.BeginHorizontal();
GUILayout.Label("Nivel de Dificultad");
if(GUILayout.Button("Facil"))
{
}
if(GUILayout.Button("Normal"))
{
}
if(GUILayout.Button("Hardcore"))
{
}
GUILayout.EndHorizontal();*/
if(GUILayout.Button("Iniciar Nueva Partida"))
{
Screen.showCursor = false;
Application.LoadLevel(3);
}
//GUILayout.Button("Cargar partida guardada(No funciona de momento)");
GUILayout.EndArea();
GUILayout.EndArea();
if(GUI.Button(Rect((Screen.width * 0.5)-75,(Screen.height * 0.5)+(Screen.height * 0.27),160,20), "Cancelar"))
{
mostrarMenuPrincipal = true;
mostrarMenuUnJugador = false;
mostrarBotonOpciones = true;
}
}
//MENU MULTIJUGADOR - PARTE 1
if(mostrarMenuModo1Multijugador)
{
GUI.Box(Rect ((Screen.width * 0.5)-160,(Screen.height * 0.5)-37,150,75),"Tipo de conexión");
GUILayout.BeginArea(Rect ((Screen.width * 0.5)-155,(Screen.height * 0.5)-14,140,50));
GUILayout.BeginVertical();
if(GUILayout.Button("Anfitrión"))
{
mostrarDialogoServer = true;
mostrarDialogoHuesped = false;
}
if(GUILayout.Button("Huésped"))
{
mostrarDialogoHuesped = true;
mostrarDialogoServer = false;
}
GUILayout.EndVertical();
GUILayout.EndArea();
}
//DIALOGO DEL MODO ANFITRION
if(mostrarDialogoServer)
{
GUI.Box(Rect ((Screen.width * 0.5)+10,(Screen.height * 0.5)-50,180,100),"Ajustes del Server");
GUILayout.BeginArea(Rect ((Screen.width * 0.5)+15,(Screen.height * 0.5)-29,170,90));
GUILayout.BeginVertical();
var JugadoresConectados : int = Network.connections.Length;
var BotonIniciarServer : boolean = true;
var BotonDetenerServer : boolean = false;
/*if(BotonIniciarServer)
{*/
if(GUILayout.Button("Iniciar Server"))
{
ArrancarServerInterno();
/*BotonIniciarServer = false;
BotonDetenerServer = true;*/
}
/* } */
/*if(BotonDetenerServer)
{
if(GUILayout.Button("Parar Server"))
{
Network.Disconnect(200);
MasterServer.UnregisterHost();
BotonIniciarServer = true;
BotonDetenerServer = false;
Debug.Log("SERVER CERRADO");
}
}*/
GUILayout.Box("Jugadores conectados: " +JugadoresConectados);
for (var level in supportedNetworkLevels)
{
if(GUILayout.Button("Empezar Partida"))
{
Screen.showCursor = false;
//Application.LoadLevel(2);
networkView.RPC( "LoadLevel", RPCMode.AllBuffered, level, lastLevelPrefix + 1);
}
}
GUILayout.EndVertical();
GUILayout.EndArea();
}
//DIALOGO DEL MODO HUESPED
if(mostrarDialogoHuesped)
{
GUI.Box(Rect ((Screen.width * 0.5)+10,(Screen.height * 0.5)-50,180,100),"Ingresa la IP del Server");
GUILayout.BeginArea(Rect ((Screen.width * 0.5)+15,(Screen.height * 0.5)-29,170,90));
GUILayout.BeginVertical();
//GUILayout.Label("Ingresa la IP del Server:");
GUILayout.TextField(InputCustomIP);
GUILayout.Box("Jugadores conectados: " +JugadoresConectados);
if(GUILayout.Button("Conectarse"))
{
Network.Connect(InputCustomIP,25000);
Screen.showCursor = false;
}
/*if(GUILayout.Button("Unirse a la partida"))
{
Screen.showCursor = false;
Application.LoadLevel(2);
}*/
GUILayout.EndVertical();
GUILayout.EndArea();
}
//OPCIONES DE JUEGO
if(mostrarOpciones)
{
//Area de fondo
//GUI.Box (Rect (5,5,(Screen.width -10),(Screen.height -10)), "Opciones");
GUI.Box (Rect ((Screen.width * 0.5)-280,(Screen.height * 0.5)-110,560,220), "Opciones");
//AREA PRINCIPAL DE OPCIONES
GUILayout.BeginArea(Rect ((Screen.width * 0.5)-275,(Screen.height * 0.5)-110,550,250));
//Comienzo de la seccion izquierda
GUILayout.BeginHorizontal();
GUILayout.BeginArea(Rect (0,30,300,200));
//CAMBIAR CALIDAD
GUILayout.BeginArea(Rect (0,0,295,40));
GUILayout.BeginHorizontal();
if (GUILayout.Button("-"))
{
QualitySettings.DecreaseLevel();
}
GUILayout.Box("Nivel de Calidad: "+ (QualitySettings.names[QualitySettings.GetQualityLevel()]));
if (GUILayout.Button("+"))
{
QualitySettings.IncreaseLevel();
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
//CAMBIAR RESOLUCION
GUILayout.BeginArea(Rect (0,50,295,175));
GUILayout.Label("Resolución");
GUILayout.BeginHorizontal();
if(GUILayout.Button("640x480"))
{
Screen.SetResolution(640,480,true);
}
if(GUILayout.Button("800x600"))
{
Screen.SetResolution(800,600,true);
}
if(GUILayout.Button("1024x768"))
{
Screen.SetResolution(1024,768,true);
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if(GUILayout.Button("1280x720"))
{
Screen.SetResolution(1280,720,true);
}
if(GUILayout.Button("1400x900"))
{
Screen.SetResolution(1400,700,true);
}
if(GUILayout.Button("1600x900"))
{
Screen.SetResolution(1600,900,true);
}
if(GUILayout.Button("1920x1080"))
{
Screen.SetResolution(1920,1080,true);
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
GUILayout.BeginArea(Rect (0,130,295,175));
GUILayout.BeginHorizontal();
if(GUILayout.Button("Cambiar a modo Ventana/Pantalla Completa"))
{
Screen.fullScreen = !Screen.fullScreen;
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
//CONTROL VOLUMEN
GUILayout.BeginArea(Rect (0,30,295,40));
GUILayout.BeginHorizontal();
GUILayout.Label("Nivel de Volumen");
GUILayout.BeginArea(Rect (110,5,180,35));
AudioListener.volume = GUILayout.HorizontalSlider (AudioListener.volume, 0.0, 1.0);
GUILayout.EndArea();
GUILayout.EndHorizontal();
GUILayout.EndArea();
//ACTIVAR TORMENTA NIEVE
/*GUILayout.BeginArea(Rect (0,125,295,40));
GUILayout.BeginHorizontal();
GUILayout.Label("Tormenta de nieve densa: ");
if (GUILayout.Button("Activar"))
{
NevDensa.SetActive(true);
}
if (GUILayout.Button("Desactivar"))
{
NevDensa.SetActive(false);
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
//ACTIVAR COPOS DE NIEVE
GUILayout.BeginArea(Rect (0,150,295,40));
GUILayout.BeginHorizontal();
GUILayout.Label("Copos de Nieve: ");
if (GUILayout.Button("Activar"))
{
CoposNieve.SetActive(true);
}
if (GUILayout.Button("Desactivar"))
{
CoposNieve.SetActive(false);
}
GUILayout.EndHorizontal();
GUILayout.EndArea();*/
GUILayout.EndArea();
GUILayout.EndHorizontal();//Fin de la seccion izquierda
//Comienzo de la seccion derecha
GUILayout.BeginHorizontal();
GUILayout.BeginArea(Rect (305,50,300,200));
//MOTION BLUR
GUILayout.BeginArea(Rect (0,0,245,25));
GUILayout.BeginHorizontal();
GUILayout.Label("Desenfoque movimiento: ");
if (GUILayout.Button("Act."))
{
Camera.main.GetComponent(CameraMotionBlur).enabled = true;
}
if (GUILayout.Button("Desact."))
{
Camera.main.GetComponent(CameraMotionBlur).enabled = false;
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
//ANTIALIAS
GUILayout.BeginArea(Rect (0,30,245,25));
GUILayout.BeginHorizontal();
GUILayout.Label("Antialias: ");
if (GUILayout.Button("Activar"))
{
Camera.main.GetComponent(AntialiasingAsPostEffect).enabled = true;
}
if (GUILayout.Button("Desactivar"))
{
Camera.main.GetComponent(AntialiasingAsPostEffect).enabled = false;
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
//SSAO
GUILayout.BeginArea(Rect (0,60,245,25));
GUILayout.BeginHorizontal();
GUILayout.Label("SSAO: ");
if (GUILayout.Button("Activar"))
{
Camera.main.GetComponent(SSAOEffect).enabled = true;
}
if (GUILayout.Button("Desactivar"))
{
Camera.main.GetComponent(SSAOEffect).enabled = false;
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
//VSync
GUILayout.BeginArea(Rect (0,90,245,25));
GUILayout.BeginHorizontal();
GUILayout.Label("Nivel VSync: ");
if (GUILayout.Button("0 (Desact.)"))
{
QualitySettings.vSyncCount = 0;
}
if (GUILayout.Button("1"))
{
QualitySettings.vSyncCount = 1;
}
if (GUILayout.Button("2"))
{
QualitySettings.vSyncCount = 2;
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
GUILayout.EndArea();
GUILayout.EndHorizontal();//Fin de la seccion derecha
GUILayout.EndArea();
}
//BOTON "Volver al Menu Principal"
if(mostrarBotonVolverMenuPrincipal)
{
if(GUI.Button(Rect((Screen.width * 0.5)-75,(Screen.height * 0.5)+(Screen.height * 0.27),160,20), "Volver al Menu Principal"))
{
mostrarMenuPrincipal = true;
mostrarOpciones = false;
mostrarBotonVolverMenuPrincipal = false;
mostrarBotonOpciones = true;
mostrarMenuModo1Multijugador = false;
mostrarDialogoServer = false;
mostrarDialogoHuesped = false;
Application.runInBackground = false;
}
}
//BOTON "Opciones"
if(mostrarBotonOpciones)
{
if(GUI.Button(Rect((Screen.width * 0.5)-65,(Screen.height * 0.5),150,23), "Opciones"))
{
mostrarMenuPrincipal = false;
mostrarOpciones = true;
mostrarBotonOpciones = false;
mostrarBotonVolverMenuPrincipal = true;
}
}
}
function ArrancarServerInterno()
{
var useNat = !Network.HavePublicAddress();
Network.InitializeServer(4,25000,useNat);
//MasterServer.RegisterHost("Sombras del Yeut", "Modo de Prueba", "Actualmente este es solo un test");
yield WaitForSeconds(1);
}
function OnServerInitialized()
{
Debug.Log("SERVER INICIADO");
}
/*function OnMasterServerEvent(mse:MasterServerEvent)
{
if( mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Server Registrado Correctamente");
}
}
function OnConnectedToServer()
{
}*/
Hope it's simple to fix, thanks in advance!
Answer by kubci98 · Jun 22, 2013 at 06:18 PM
There is only one TextField in script (line 221). It shouldn't be even drawn, because there is no position or scale. See TextField documentation and try to edit it.
You were right, now i understood and got the IP input field working, thanks a lot!
btw it looks you are new to unity. It is good to mark every answered question as closed.
This answer is wrong. GUIlayout don't need position or scale (it positions and scales it self). You just wrote it wrong, the correct way is:
StringVar = GUILayout.TextField (StringVar,#);
"#" representing the max number (an int) of characters the user is allowed to type inside the text field and "StringVar" the string variable you want to edit.
In the case of the line 221 it would be:
InputCustomIP = GUILayout.TextField(InputCustomIP,12);
Check GUIlayout.TextField for more information: GUILayout.TextField
"GUI.TextField" and "GUILayout.TextField" are two different things
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
script to save and load a game 3 Answers
Main Menu Button Help! 1 Answer
Main Menu scrpit not working 1 Answer
creating a menu 1 Answer