GUI is too sensitive

Alright, this question has been bugging me for a while. It is really specific to my code so search as I have, I couldn’t find anything helpful save a few articles about clicking on GUI elements.

So here is what’s happening: I choose place a tower on the grid (tower selection is on a drop-down GUI at the top of the screen), then I choose the green-line “selector.” I select the tower I just placed putting an orange arrow and a white radius circle around the tower. When I click on any of the GUI (even a box), the tower deselects. But when I debug, all the variables are as they should be. I have even commented out large blocks of code to find the troubled area to no avail.

I have posted the current build of the game here.

The code in question is attached to my main camera, and is located here. I am really bad at commenting code when I am the only one that will see it, so I will start commenting it right away and upload that version asap.

With those resources I ask my question: Why does the tower deselect? What am I doing wrong? Is it a glitch with Unity’s code conversion? How can I get the effect I want?

In the mean-time, I will comment my code and continue reading other articles in the off chance that someone else has encountered this problem. Also, if you need more info, I am happy to offer anything that will help solve this problem.

Just because you click on a gui button that doesn’t make this code invalid:

if (Input.GetMouseButtonDown(0))
    if (mouseOnScreen)
        isSelected = false;

You already check if the mouse is within the screen, but you should ignore the click as well when you’re inside a GUI.

You can use the Rect.Contains function to test if the mouseposition is within the GUI.

This test should be done inside OnGUI. Event.current.mousePosition holds the mouse position in GUI coordinates (0,0 == top left):

overGUI = Rect(Screen.width - 500, Screen.height - 94, 500, 94).Contains(Event.current.mousePosition);