- Home /
Duplicate Question http://answers.unity3d.com/questions/20328/onmousedown-to-return-object-name.html
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 ?
Answer by kacyesp · Aug 27, 2014 at 08:47 PM
Seems like someone has had your same question: http://answers.unity3d.com/questions/20328/onmousedown-to-return-object-name.html
Do you $$anonymous$$d accepting the answer? :D Trying to get my rep up :)
Answer by Demmy · Aug 27, 2014 at 10:22 PM
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;
Follow this Question
Related Questions
enable/disable object in heirachy from another object 1 Answer
JS, get child 2 Answers
enable/disable child objects? 2 Answers