objects behind GUI gets selected

how to prevent this? if i have object that is behind gui, clicking on the gui works but also objects behind gui responds.

should i use tags? can gui have a tag?

Hard question.

Any GUI element should have a Rect variable. You could make a list containing all Rect of presents GUI elements.

When you click on a GUI texture, and a box right behind, the script of the box should include the detection.

The detection would consist in retrieving the mouse position, and checking whether a Rect was present (the function Contains). If true, well, the box don’t do anything. If false, it means there was no GUI element so the box can go on.

Any gameObject can have a tag, but the tag won’t prevent you, or give you a hint on whether you wanted to check for the GUI element or the box.

Your opinion ?

i can check in my routine, that handles clicks, if both elements are clicked, if so only one, that has priority, gets selected. in this case GUI. but the problem is that i cant tag GUI elements, like all other game objects. or am i wrong?

You can’t tag gui elements made by GUI or GUILayout.

I am curious to see your “routine that handle click”…

“routine that handles clicks” is just for 3d objects in the scene, it doesn’t handle gui. it casts ray and catches objects that are below the mouse pointer…in this routine i could check if gui is clicked (i dont know how) and then i would not mind to check if something else is below the gui since gui has priority

Well, go on with that script, and simply check first whether a GUI element is here, as I told you( Input.mousePosition + Rect.Contains), before checking for 3D Objects.

ok i will try it and get back to you

i tried it and it works, sort of…i realized that
somehow coords are inverted. it works but it doesnt works in the region where it should work.

i made a little test with this line in OnGUI:

GUI.Label(Rect(Input.mousePosition.x,Input.mousePosition.y,100,30),Input.mousePosition.ToString());

the label should follow the mouse, but when i move mouse up label goes down… so what is going on here??

Check script reference

mousePosition 0,0 starts in the bottom left corner.

so replace mousePosition.y with: Screen.Height - mousePosition.y

btw you can also use GUI.enabled = false to disable different GUI controls. I use this for popupscreen and to manage different screens to disable the screen and buttons behind it.