How do you click on something in 3D?

The mouse is flat. Your world isn't.

How do you click on something in 3D?

The problem

A screen position isn't a world position.

void Update(){    if ( !Input.GetMouseButtonDown( 0 ) ) return;     Vector3 p = Input.mousePosition;    SelectUnitAt( p );   // ( 812, 433, 0 )}

mousePosition is pixels on your screen. Your units live in meters, out in the scene.

The fix

Fire a ray through the pixel.

The camera turns a screen point into a ray: from the lens, through the cursor, into the scene.

pixel→ScreenPointToRay→Raycast→what you clicked

The fix in code

Ray, then Raycast.

void Update(){    if ( !Input.GetMouseButtonDown( 0 ) ) return;     Ray ray = cam.ScreenPointToRay( Input.mousePosition );    if ( Physics.Raycast( ray, out RaycastHit hit ) )        Select( hit.collider.gameObject );}

cam is cached in Awake. hit.point is the exact spot on the object you clicked.

Watch out

No collider, no click.

Raycasts only hit colliders. A mesh with no collider can't be clicked.

Mesh, no collider
the ray goes through
Add a collider
now it's clickable
Skip some things
pass a LayerMask

The rule

Click in 3D with a ray.

Make the ray
cam.ScreenPointToRay
Find the hit
Physics.Raycast
It needs
a collider