In 3d I have a wall of Primitive Cubes. If I mouseover a cube I would like to know how I can colour the cube below the mouseover cube to a different colour with unity3d
You could probably raycast from the clicked cube down to the next cube, but a far simpler logic-only solution would be to set up all of your cube references in an array of arrays (also called a 2-dimensional array). If you do that, you can select the “cube below” just by subtracting 1 from the row index.
if (Physics.Raycast(hit.transform.position, Vector3.down, out hit))hit.collider.gameObject.transform.GetComponent<Renderer>().material.color = new Color(0.1f, 0.0f, 0.5f);
Looks fine, aside from really needing to be spread across multiple lines and not crammed into one line like this- and assuming that “hit” is the same name as the RaycastHit that resulting from the current cube being hit. You should change Vector3.down to hit.transform.down though, I believe.
you could also check for OnMouseEnter/OnMouseOver/OnMouseExit message on your cubes if you don’t intend to do touch screens.
Alternatively, if your cube wall is of known size and relative to screen size you could i simply divide cursor position by cube size to find out which cube is targeted (very effective performance wize if that’s to ever be a concern to your project).