- Home /
drawing a box with GUI
function OnGUI() {
print(""+boxselecting);
if(boxselecting){
var width : int = leftupbox.x - rightdownbox.x;
var height : int = (Screen.height - leftupbox.y) - (Screen.height - rightdownbox.y);
var rect : Rect = Rect(rightdownbox.x, Screen.height - rightdownbox.y, width, height);
//GUI.Box(rect, "Its ALIVE");
GUI.Box(Rect(rightdownbox.x, Screen.height - rightdownbox.y, width, height), "Its ALIVE");
//GUI.DrawTexture(rect, selectionTexture, ScaleMode.StretchToFill, true, 10.0f);
print("GUI");
} }
this is my code for drawing a box and unity gives me the GUI can only be called in OnGUI error whats wrong with my code?
Answer by denewbie · Nov 16, 2010 at 10:43 AM
I don't know how well you pasted this piece of code but your "function OnGUI()" is missing the "f".
If thats the same as in your code that thats prob the problem?
Ok i ll check it out for you then but do oyu have any errors?
Oh one more thing. The Input values for "Rect" should all be float and not int so you might want to check that out.
Answer by diabloroxx · Nov 16, 2010 at 04:22 PM
You have to Post the entire code to ascertain what is going wrong where. Have you called any other GUI object or referenced any GUI element from any other function. If so, remove that.
private var mouseposition : Vector3; private var boxselecting : boolean = false; private var leftupbox : Vector3; private var rightdownbox : Vector3;
// semi transparent texture for the selection rectangle var selectionTexture;
function Update () { if(Input.Get$$anonymous$$ouseButtonDown(0)){//0 is linker,1 is rechter,2midden mouseposition = Input.mousePosition; if(!boxselecting) leftupbox = mouseposition; boxselecting = true; } if(Input.Get$$anonymous$$ouseButtonUp(0)){ boxselecting = false; mouseposition = Vector3.zero; } if(boxselecting){ rightdownbox = mouseposition; } } function OnGUI() { print(""+b
Your code seems to run properly in my system. However, since there is no proper instantiation of the rightdownbox and leftupbox , I don't see any GUI. But I definitely did not get the " GUI can only be called in OnGUI error" . Try disabling this script and see if you have any random code elsewhere in your project. Else, export your package and post it somewhere so that we can check for other errors.
Answer by Max Kaufmann · May 09, 2011 at 09:24 PM
I found GUI.Box() to be very trap-laden, my solution was to eschew it entirely, and just use GUI.DrawTexture() with a 1x1 colored texture image.