How to make GUI box act as buttons

hi…there…i am trying to make a HUD for my game…where once a gui texture is clicked…it drops down 4 gui boxes…i want to make these boxes further click able…can someone check my code and tell me where i am going wrong…the gui boxes don’t give out any feedback except for a print

here’s the code

var normalTex : Texture2D;

var hoverTex : Texture2D;



var box : boolean = true;

var mouseInside  = true;

var rect = (Rect(100,100,50,50));

function update ()

{

if (rect.Contains(Input.mousePosition))
mouseInside = false;

 print ("inside");

}

function OnGUI ()
{
GUI.color = Color.blue;
if (box == false)
{

			//GUI.Box(Rect(100,100,50,50), hoverTex);
			GUI.Box(Rect(rect), hoverTex);
			GUI.Box(Rect(100,150,50,50), hoverTex);
			GUI.Box(Rect(100,200,50,50), hoverTex);
			GUI.Box(Rect(100,250,50,50), hoverTex);
			
		}

}

function OnMouseEnter ()
{
guiTexture.texture = hoverTex;

}

function OnMouseExit()
{
guiTexture.texture = normalTex;
}

function OnMouseDown()
{
 guiTexture.texture = normalTex;
 box = !box;
}

2 Answers

2

Why don’t you use GUI.Buttons instead of boxes ? Buttons are great to act as buttons :stuck_out_tongue:

This is not helpful, there are lots of reasons to use gui boxes instead of gui buttons.

I don’t know if it too late, but I have an idea to make your GUI.Box clickable. Why don’t you make GUI.Button over your GUI.Box and make it invisible. So you will see only your GUI.Box and you can click on it.

Assume that you have GUI.Box and then make GUI.Button with invisible on your GUI.Box

// Your GUI.Box that you want to clickable
GUI.Box(Rect(100,150,50,50), hoverTex);

// Create Invisible Button over your GUI.Box
if (GUI.Button (Rect(100,150,50,50), "", GUIStyle.none)){
    // Do what ever
    Debug.Log ("GUI.Box pressed");
}

Hope this can help you or anyone who face this problem.

I apologize if there are any errors.