Stop RayCast Passing thru GUI

I have a GUI where players select what they want to place in the game world. Once selected a ScreenPointToRay places the object in the world on mouse click.

The problem is that when you click the button, the ray passes thru the GUI and places the object in the world under the button.

Is there a way to stop the ray passing thru the GUI?

For this answer i presume you are using OnGUI and that isnt going to detect your raycast so it Sounds to me like you need a simple state machine for placing the items. When the button is clicked set a Boolean value = true then don’t do your ray cast until you find that value. Reset it to flase when you have placed your object. I presume at the moment the raycast is always happening.

You should be using a layer to define the GUI object that you do not want to raycast against.

This way, when you do the raycast, you will tell unity to exclude any object that is set with that layer.

Layer mask:

http://unity3d.com/support/documentation/Components/Layers.html

Raycast:

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

The way I get it, you wants the gui to lay in the corner and be able to click on it any time and place the object without any other manipulations.

Maybe you could try to check the position of the mouse pointer on the screen and when clicking if it is at a certain position (gui position), then you have the gui options, any other position you are placing on the world using raycast.

check this question

i made it through GUIUtility.hotControl