Deselect selected object

Hi all !

So i need some help to solve my problem :slight_smile:
In my game, when i select a npc (i use Raycasting on a collider in a specific layer) a “popup” appear with his name and some others things (my next step is to design a dialog box with many choice).
To deselect, the player must click somewhere on the screen where there is nothing selectable (like WoW for example).
So i have that code, and it works properly :

if (Input.GetMouseButtonDown(0))
{
 Ray ray = cameraPlayer.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 //Selectable layer is 10
 LayerMask layerMask = 1 << 10;
 if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
    {
    //deselect the current selection
    if (currentSelection != null)    
     {
    currentSelection.SendMessageUpwards("DeSelected");
     }
     //tell to the hit object that he is selected    
     currentSelection = hit.collider.gameObject;
     currentSelection.SendMessageUpwards("Selected");
     }
     else
     {
      //deselect the current selection
      if (currentSelection != null)
        {
       currentSelection.SendMessageUpwards("DeSelected");
         }
      }
}

The problem is that, when i click on the “pop up” (some GUI elements), npc is deselect to.
What i want is to keep the selection active to be able to use button.

I suppose there is probably a solution with the layers but how specify a layer for GUI element ?

Thank you in advance :slight_smile:

OK, my brain was probably blocked…one more coffee and i find a solution…
→ if my npc is selected, check the position of the click and if on the “popup”, not raycat xD

But if someone have an other solution, i’m interested ^^

I like your solution … but I’m pretty sure a raycast doesn’t hit a GUI element. They’re 2 different visual systems (at least in my mind). So I’m not sure why the problem is occurring.

But I also have a question, about this line:

LayerMask layerMask = 1 << 10;

What does it mean??? What’s with the two “less than” symbols, << ? I have never seen that and I have the feeling it’s useful!

Bit shifting:

var foo = 2;	// 2 = 00000010
foo = foo << 1;
print (foo);	// 4 = 00000100
foo = foo >> 2;
print (foo);	// 1 = 00000001

–Eric

Selecting by Layers really helped me out, good post :slight_smile: