OnGui is not called.

Hi,

I’m new to Unity and tried the “3DPlatformTutorial”. The first thing is, that the example code is not 100% compatible with the current version of Unity since a clobal name was used for a variable. But, this was not really hard to fix.

Now, I’d like to add the StarMenu. However, adding the StartMenuGui-script to the camera seems not to work as expected. The “OnGui()” method seems to be never called. I even added a output to the log to confirm this.
Here is the script code:

#pragma strict

// Make the script also execute in edit mode
@script ExecuteInEditMode()

var gSkin : GUISkin;
var backdrop : Texture2D;
private var isLoading = false;

function OnGui() {
	Debug.Log("Called!");
	if(gSkin) {
		GUI.skin = gSkin;
	} else {
		Debug.Log("StartMenuGui: GUI Skin object missing!");
	}
	
	var backgroundStyle : GUIStyle = new GUIStyle();
	backgroundStyle.normal.background = backdrop;
	GUI.Label(Rect((Screen.width - Screen.height * 2) * 0.75, 0, Screen.height * 2, Screen.height), "", backgroundStyle);
	
	GUI.Label(Rect(Screen.width / 2 - 197, 50, 400, 100), "Lerpz Escapes", "mainMenuTitle");
	
	if(GUI.Button(Rect(Screen.width/2 - 70, Screen.height - 160, 140, 70), "Play")) {
		isLoading = true;
		Application.LoadLevel("TheGame");
	}
	
	var isWebPlayer = Application.platform == RuntimePlatform.OSXWebPlayer || Application.platform == RuntimePlatform.WindowsWebPlayer;
	if(!isWebPlayer) {
		if(GUI.Button(Rect(Screen.width/2 - 70, Screen.height - 80, 140, 70), "Quit")) {
			Application.Quit();
		}
	}
	
	if(isLoading) {
		GUI.Label(Rect(Screen.width/2 - 110, Screen.height/2 - 60, 400, 70), "Loading...", "mainMenuTitle");
	}
}

Interesting thing is, if I change the method to “Start()” it is called. With errors of course …
If it helps, I’ve added a screenshot of the “Inspector”.

Thank you in advance!

1 Like

“OnGUI”

Capitals. Capital!

1 Like

Thanks alot.
I’m felling very stupid now.

1 Like

Nope. I yell at myself every day for missing a capital letter. I swear they’re out to get me.

1 Like

Syntax highlighting is your friend…

–Eric

Syntax highlighting would help if it would highlight function names. Using Unity’s builtin MonoDevelop thingy I don’t see an option for doing that.

I wouldn’t be suprised to be told I"m doing it wrong, though. On the plus side, my dev environment is nicely rainbow-colored now. xD

LOL! thanks for this thread. i just stupidly made EXACTLY THE SAME MISTAKE lol
was wracking my brains trying to fix this!

Fellow case-frustrated programmer here, thanks for saving me (probably hours) of time :slight_smile:

^ what he said :slight_smile:

I am having the same type of problem except the capitalization is correct.
var btnX:float;
var btnY:float;
var btnW:float;
var btnH:float;
function start(){
btnH=Screen.width0.2;
btnW=Screen.width
0.2;
btnX=Screen.width0.1;
btnY=Screen.height
0.1;
}
function OnGUI(){
Debug.Log(“called”);
if(GUI.Button(Rect(btnX,btnY,btnW,btnH), “Start Server”))
{

}
}

its not even being called.