- Home /
Null exception when drawing a rectangle
I am trying to create a box that has 1/6 of a screen margin on the left and right side and extends from the bottom of the screen to 1/6th of the way to the top. I don't get any errors on compile but when I run I get a runtime error of "object reference not set to an instance of an object. Since I called new rect inside of the GUI.Box I don't know why I am getting this error. I have tried looking at the declaration of box in Gui basics http://docs.unity3d.com/Manual/gui-Basics.html.
void Start () {
float top = Screen.height / 6;
float leftSide = Screen.width / 6;
float rightSide = Screen.width - leftSide;
float bottom = 0f;
GUI.Box(new Rect(bottom, leftSide, rightSide, top),"this is a test");
}
Answer by Graham-Dunnett · Nov 17, 2014 at 09:08 PM
You read the docs, but missed the first, most important sentence:
"UnityGUI controls make use of a special function called OnGUI()
."
GUI
functions can only be called inside OnGUI()
.
Alright, thank you, after implementing OnGui it is not displaying anything. Is there any reason you would see that would be an indicator as to why it would not actually show the rectangle? I know this is outside the scope of the question but it doesn't hurt to ask right?