void SelectObjects() { Ray ray = GetComponentInChildren <Camera>().ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { // the object identified by hit.transform was clicked // do whatever you want currentLaserSelected = hit.collider.GetComponent <LaserController>(); if (currentLaserSelected == null) { if (lastLaserSelected != null) { lastLaserSelected.Selected(false); lastLaserSelected = null; } pointerHit = false; } if (lastLaserSelected == null && currentLaserSelected != null) { currentLaserSelected.Selected(true); lastLaserSelected = currentLaserSelected; pointerHit = true; } if (currentLaserSelected != lastLaserSelected) { currentLaserSelected.Selected(true); lastLaserSelected.Selected(false); lastLaserSelected = currentLaserSelected; pointerHit = true; } } else { if (currentLaserSelected != null) { currentLaserSelected.Selected(false); } currentLaserSelected = null; lastLaserSelected = null; pointerHit = false; } }