Scene View - Click selects wrong object?!

I am facing a very frustrating bug for a very long time. Sometimes (45% of times), when there are a few objects (like cubes etc.) layered on top of each other (like Enviro meshes, and terrain), mouse-clicking on anythng wil select not the thing I am pointing at, but instead it selects the “lowest” or “largest” object the click “penetrated”.

Imagine a cube on a Unity Terrain. if I look from up and click on the cube, in 45% of cases not the cube will be selected, but the terrain underneath it. To select the cube I will have to awkwardly position my camera in a way that nothing is behing the click-line but the cube.

How do I fight this issue? It’s destroying my nerves. ;/

I’m not sure what’s you actual problem- The fact that when you click multiple times at the same point, Unity cycles through all objects that intersect the “ray” of the mouse pointer. That’s actually a feature and in many cases very useful.

However there are ways to prevent certain interaction. You can use layers to either prevent selection or hide the objects of the layer completely in the editor. The editor has a “Layers” dropdown at the very top right which controls which layers are visible and which are selectable.

While locking a layer usually works just fine, there seems to be one exception: Terrains. Locking the layer of a terrain object doesn’t seem to prevent selecting it. However you can still simply hide the layer so the terrain isn’t visible anymore. It’s also possible to simply deactivate the terrain temporarily. Though in most cases, if the top most object is already selected and you click again so it selects the next lower object, simply continue clicking until your desirec object is selected again. How many object so you have on top of each other?

So again, the behaviour of the SceneView is deterministic and i never had any problems with it. What might confuses a bit is that an object selection can have two different “selected states”. It generally works like this:

  • If nothing is currently selected, when you click the top most object as seen from the camera is selected. So that object you have your mouse on. That object will get the “selected” state as well as the “hot” state (i just made up that term).
  • if an object is currently selected and has the hot state, when you click directly on another object, that object will be selected and receives the hot state, just like in the first case.
  • However when you click on a hot and selected object again the selection goes to the next lower object that the mouse ray hits. Note that the hot state stays at the top most object. Each time you click again on the hot object the selection goes one step down to the next object until the last is reached where it will select the top most again.
  • When you indirectly selected an object by clicking the hot object twice so the second lowest object is selected (the top one is still hot), then you now directly click on the currently selected object it sill stay selected and will become hot. Every further click will again propergate the selection further down.

Have you tried layers? Maybe mess around with that.

You could try different things. Regarding lack of some information on your situation, i would choose these solutions:

  • Use ‘Physics.RaycastAll’ to get an array of all colliders which got hit by the ray
  • Visualize the Ray by using ‘Debug.DrawLine’ or ‘Debug.DrawRay’ and then pause the game with ‘Debug.Break’
  • Use Layers for the terrain and object you are trying to select

Maybe you can get around it using a LayerMask on the raycast(), put the cube on that specific layer.

int CubeLayerMask = 1 << 9; 

Where 9 would be the layer number for your cube passed into the Raycast layermask parameter.