why won't any of the OnGUI() scripts work?

For some reason Unity will not execute any of the GUI functions. I'm not exactly new to coding, and I can't even get a simple box with text to work, even when I'm using code that I pulled straight from the Unity scripting reference:

function OnGUI() { GUI.Box(Rect(0,0,Screen.width,Screen.height),"This is a title"); }

I've tried several of the basic GUI functions and none of them will display on the game screen at runtime even though I don't have any errors in the script. Not sure if theres some kind of wierd setting I haven't found (and I've looked), its just getting very frustrating trying to work in a new engine and having something as simple as the GUI functions not work at all. If anyone can help me out it would be greatly appreciated!

6 Answers

6

I was having the same problem. Turns out, I was spelling it OnGui instead of OnGUI.

lol I did this exact thing. Thanks!

Make sure you actually attach the script to the camera as well. Drag the script from the library and drop it on the camera. In the properties inspector the last entry will show you have a script there when you select the camera.

This doesn't matter.

Exactly, but it is not needed to be attached to the camera object

As I post in my answer it can be whatever GameObject

Hi,

Have you created an empty GameObject (GameObject->Create Empty) and the drag-and-drop the script from the project to the NewEmptyGameObject in the Hierarchy?

It functioned perfectly when I tried it.

Maybe you have some other compile errors that interfere in other scripts? Check your console window if any errors are present and fix them. Other than that I can't think of anything else than to make sure your game object is enabled and not disabled. You could always try to right click the script and select reimport in case changes in the file weren't picked up by unity and of course make sure you saved the script file :)

The script was attached to an empty game object when I tried it. All the other scripting components I've tried using like transform.Translate or transform.Rotate work fine and relate to the object the script is attached to, but still nothing as far as the GUI goes. Quite frustrating, is there any other possible problems that this could relate to? Maybe a missing install package or something? Thanks for the help thus far though, much appreciated.

Do you have OnGUI inside of another function? That can really mess things up.