First we need to create a Unity Layer. In the top right corner you'll find a dropdown with all the layers in your project. You can choose to edit this list.
Select the objects you want to hit with the raycast and assign them to the layer you will be targetting.
//This will assign layer 8
private int targetLayer_ = (1 << 8);
//This will assign layers 8 and 10
private int targetLayer_ = (1 << 8 | 1 << 10);
//create a RaycastHit that will collect the information about the collision
RaycastHit mousehit = new RaycastHit();
//get the cursor coordinates
Vector2 mouseCoords = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//Physics.Raycast returns true if it hits the target layer
if( Physics.Raycast(Camera.main.ScreenPointToRay(mouseCoords), out mousehit, 1000.0f, targetLayer_) ){
Debug.Log("Hit target");
}else{
Debug.Log("Missed target");
}