Error: Unable to execute valid code due to unrelated error

It just about says that. Here’s the exact error:

And here’s the code in question:

function OnGUI ()
{
	GUILayout.Space(Screen.height-20);
	GUILayout.BeginHorizontal();
	if(GUILayout.Button("Send") || Input.GetKeyDown("return"))
	{
		//Debug.Log(text);
		networkView.RPC("AddMessage",RPCMode.All,networkView.viewID,text);
		text = "";
	}
	text = GUILayout.TextField(text,GUILayout.MinWidth(300));
	GUILayout.EndHorizontal();
	
	GUILayout.Space(-20);
	
	for(var message in messages)
	{
		GUILayout.Space(-35); // Line 22, the antagonist
		GUILayout.Label(message);
	}
}

Oh, and it was working perfectly before I added the Input command, the only change. I don’t get it.

“You got me. By all accounts it doesn’t make sense.” -Kronk

Input isn’t compatible with OnGUI, which can draw several times per frame and gets messed up if you change stuff in the middle of that; use Event instead. Use Input in Update or other functions.

–Eric

Thank you. (that was fast)
This GUI System is nice, but I keep running into nuances, like this, that are getting irritating. It looks like I’m going to have to use it solely as a view layer, like Cocoa’s drawRect. Fortunately, I can use the same MVC principles from Cocoa as well, making for a quick fix. I just wish I was told about this earlier, all the docs I’ve seen seem to have treated it like Update, hence the confusion.
Oh well, one step backward, two steps forward :wink:

Not necesssarily.

var doSend = false;

function Update()
{
   if( doSend || Input.GetKeyDown("return")) 
   { 
      doSend = false;
      //Debug.Log(text); 
      networkView.RPC("AddMessage",RPCMode.All,networkView.viewID,text); 
      text = ""; 
   } 
}

function OnGUI () 
{ 
   GUILayout.Space(Screen.height-20); 
   GUILayout.BeginHorizontal(); 
   if(GUILayout.Button("Send")) 
   { 
      doSend = true;
   } 
   text = GUILayout.TextField(text,GUILayout.MinWidth(300)); 
   GUILayout.EndHorizontal(); 
    
   GUILayout.Space(-20); 
    
   for(var message in messages) 
   { 
      GUILayout.Space(-35); // Line 22, the antagonist 
      GUILayout.Label(message); 
   } 
}

Sorted!

Your point doesn’t make sense, so I think I misphrased my previous statement. What I meant to say was that I need to treat the OnGUI function as view layer only. I tried doing simple logic with it a while back, and ran into that problem as well, hence my conclusion that OnGUI is unsuitable for program logic, contrary to what the docs imply.

I reiterate that point about the docs being misleading. As it clearly states in the GUI basics guide:[quote]
The OnGUI() function gets called every frame as long as the containing script is enabled - just like the Update() function.
[/quote]This implies that the OnGUI function, like Update, is called only once per frame. It’s reinforced by the fact that most of the examples put everything in the OnGUI function. However, in reality, the OnGUI function is called multiple times per frame at seemingly random intervals. This makes it not only a bad idea, but, as I have seen from personal experience, impossible to use for actual logic. OnGUI, therefore, should only be used as a view layer, with other, more reliable, functions executing the logic. Basically, a Model-View-Controller design.

This kind of stuff should be in the manual! :lol:

I disagree.

OnGUI should not be used for logic which either handles resources which are not supported (like Input) or depends on a false concept on how and when OnGUI is called. Apart from that, OnGUI is quite suited for logic and not only display commands.

If you wish to discover how the calls to OnGUI are not random, you should try and output Event.current.type in the beginning of it and study the results.

I do agree that the quoted documentation, while not strictly incorrect, is flawed and should be clarified. Did you file a bug report on it?