GUILayout.TextField problem

Hi there,

This may be a thread for the support subforum, but I made a shot here in the scripting subforum.

I currently got two JavaScript scripts in my project. One of them handles all the network communication and stores all the variables used (these are of course public variables), and the other script is simply a menu script that draws GUI layouts and handles menu navigation - and calls public functions on the network-script.

The problem lies within using a specific line of code. At the start of my script, in the Awake() function, I’ve used GetComponent for the network-script to access the menu-script, and vice versa. The variables to these are declared at the beginning of the script, outside any function (as in exposed variables).

The following code causes the problem; my program simply crashes when I try to run a test of it within the Unity Editor:

GUILayout.BeginVertical();
var userName : String; // Temp var
userName = GUILayout.TextField(userName, GUILayout.MinWidth(110));
GUILayout.EndVertical();

This code is of course embedded within a function and the function is being called perfectly. When I comment the above code out, the entire script is flawless and runs like it should. But when I uncomment it and try to use it, the program hangs for a few seconds after pressing “Play” and then Windows tells me the program encountered a problem, and then the Windows problem solver appears.

This has happened every time now, except when I do the following:

GUILayout.BeginVertical();
var userName : String; // Temp var
userName = GUILayout.TextField("", GUILayout.MinWidth(110));
GUILayout.EndVertical();

But then, of course, I can’t edit the textfield like this. It simply does not like the fact that I try to edit a variable within GUILayout.TextField().

Does anyone have a clue as to what may be crashing my editor?

Many thanks for any replies.

I can also mention that the console does not post a single error, which is why I am a bit lost as to why this happens.

Well, after tinkering around, making the desired target variables static in the main script, and stuffing all variable value assignments into the Start() function of each script, it currently works.

Program does not crash anymore, but I still don’t know why it did.

//Try to put codes inside OnGUI function

// and take the userName var outside the function like this:

var userName : String; // Temp var
function OnGUI () {
GUILayout.BeginVertical();

userName = GUILayout.TextField(userName, GUILayout.MinWidth(110));
GUILayout.EndVertical();

}

@uhahaha
The code pasted was taken out of the function they were embedded in, there was nothing wrong with the code structure. Only a rogue problem that crashed my editor, but now it’s fixed and working as intended.