I’m setting up a toolbar to make various objects visible/ Invisible and to switch between cameras. I have GUI.Box with Gui.Buttons overlayed as described in the reference manual. The problem is that the buttons are appearing behind the Box. They operate fine but it’s fairly obvious that they’re not where they should be.
I’m brand new to scripting and am quite amazed I have got as far as I have but I could do with working out how to assign these GUI.Buttons to a layer so that they render in the correct order. My script for toggling the objects on and off is below, this works fine but am not sure where or what to put in to assign layers to the buttons.
var Object1 = “Cube”;
var ButtonLabel = “”;
var ButtonX = 220;
var ButtonY = 40;
var ButtonWidth = 80;
var ButtonHeight = 20;
function OnGUI () {
if (Object1 != “”){
if (ButtonLabel == “”){
ButtonLabel = “Toggle” + Object1;
}
var ObjectToToggle = GameObject.Find(Object1);
var ObjectRenderer = ObjectToToggle.GetComponent(Renderer);
if (GUI.Button (Rect (ButtonX,ButtonY,ButtonWidth,ButtonHeight), ButtonLabel)){
if (ObjectRenderer){
ObjectRenderer.enabled = !ObjectRenderer.enabled;
}
}
}
}
Thanks in advance.
Andy.
Hi, welcome to the forum!
Did you put the call to GUI.Box above the GUI.Button calls in the code? If there are conflicts between elements that you can’t fix by changing the rendering order, you can generally sort it out by putting the conflicting elements in different scripts. In this case, for example, you could put the box in a second script and set the value of GUI.depth to a lower value (eg, set it to 1 in the button script and 0 in the box script).
Thanks for the welcome, I may be quite regular from now on!
Yes I do have the GUI.Box and GUI.Button in different scripts.Changing the GUI.Depth sounds like it would work, I wasn’t sure of the paramater that would affect the drawing order but I’ll give that a try. Sorry to sound like an imbecile but where would I specify that in the code? Like I said I’ve only been scripting for about 3-4 days and have got to where I am by picking apart existing scripts and trying to work out how they’re put together.
Alternatively are you saying if I put the call for both the box and the button in the same script but in the right order then this will affect how they are rendered?
Cheers
you just have GUI.depth = xxx in each OnGUI that defines this OnGUIs call layer.
within the same layer (OnGUI call) the order in which the command appear defines the depth. the later the further in front
Unless I am mistaken, GUI functions can only be used in OnGUI () function.
So :
function OnGUI ()
{
GUI.depth = 2.0;
}
Edit : sgrunt.
Sorry to sound like an idiot but where would I put the
‘GUI.depth = xx’ in the code?
Ah! Fairly obvious then.
Thanks everyone, I’ll give it a try.
Funny, the GUI.depth actually works, but just keep in mind that lower values are drawn on top of higher values. Using GUI.depth and assigning the right depth values enables you to have several different scripts with OnGUI calls and be able to render the GUI elements from each in the desired right order.