How to get name of the Object with click ?

Hello guys i’ve attached “OnMouseDown” function to my script but i don’t know how to get my object’s name when i clicked to it. So is there a way to get that ?

Seems like someone has had your same question:

RaycastHit hit;
void Update()
{
if(Input.GetAxis(“Fire1”)!=0)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit,2f))
{
ourObject=hit.collider.gameObject+;
Debug.Log ("object that was hit: "+ourObject);
}
}
}

-replace Fire1 with your input, in case you deleted Fire1

-objects that you hit MUST have collision

-“2f” in raycast is the distance of ray (float), put your desired value

-if you wish to get the parent of an object use

ourObject=hit.collider.gameObject.transform.parent.gameObject;