Skip to content

Instantly share code, notes, and snippets.

@tblack-studio
Created September 29, 2017 11:06
Show Gist options
  • Save tblack-studio/a79554a3b7d75478cee8cc2ec64be4db to your computer and use it in GitHub Desktop.
Save tblack-studio/a79554a3b7d75478cee8cc2ec64be4db to your computer and use it in GitHub Desktop.
Print Unity Game Object Name when clicking to them
if (Input.GetMouseButtonDown(0))
{
//empty RaycastHit object which raycast puts the hit details into
var hits = new RaycastHit[0];
//ray shooting out of the camera from where the mouse is
var ray = Camera.allCameras[0].ScreenPointToRay(Input.mousePosition);
if ((hits = Physics.RaycastAll(ray)).Length > 0)
{
//print out the name if the raycast hits something
foreach(RaycastHit hit in hits){
Debug.Log(hit.collider.name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment