3dimensional selection of gameobjects

I would like to have gameobjects on a scene scattered around but I would like one selected objects which I can colour or delete but I also would like to switch objects with the arrow keys and I’m not sure how to switch to the next object, ie.

      ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      if(Physics.Raycast(ray, out hit)) {
       var n = hit.transform.name;
      if( n == transform.name && Input.GetKeyUp("a")){
            selectedc = ?????
            selectedc.transform.GetComponent<Renderer>().material.color = new Color(0.1f, 0.0f, 0.5f);
      }
      }

That would select the object closest the left of the object and I would need to change hit to selectedc.
Also in a 3dimensional view if Im looking at a pyramid of objects from the topview down I would like to select the top most object of them when I go left, ie. The closest to the camera. I dont want to go deeper, I want closer to camera.

Any clues appreciated thanks!

you don’t want to nest the input if within the raycast if, that would need the user to press the key in the exact same frame as the mouseover, and then in the next frame the mouseover will reset the selected object.

Have a reference to “selectedObject”, then handle each input case separately,

if user mouse over a transform => set selectedObject to the mouseover’d transform
if user pushes button and there is a selectedObject and there is another object in that direction => change selectedObject
etc.

these are seperate cases and therefore there is no need for nesting