i have 6 cubes under an empty game object. the OnMouseDown() function gets called for all of those you click on them. that is they show up in the hierarchy as being under another item.
now i’m trying to add OnMouseDown to a cube that’s a child of another cube but it never fires those OnMouseDown functions. what am i doing wrong?
the only thing that i can find that breaks it is it’s location in the hierarchy. moving one that works under the cube breaks it. taking it out fixes it. only i need it to work in the cube.
If a parent object has a rigidbody, then all colliders on its children essentially become part of the parent object (known as compound colliders), and the children are no longer considered separate objects as far as collision goes. In this case OnMouseDown will only work if it’s attached to the parent, and clicking on any child colliders will cause OnMouseDown on the parent to fire.
I think it doesn´t work because OnMouseDown is tied to the primary gameObject, when you have place things under a cube then the activy parent gameObject becomes that cube, and since that is not the gameObject you clicked on it, it doesn´t work.
What you could use as a workaround is to have the cube on its OnMouseDown startCorroutine or sendMessage on all its gameObjects children.
This is the correct answer, I did a test case and I was having this very problem. What I had to do is propagate the OnMouseDown, OnMouseUp OnMouseDrag events to my child scripts.
Example:
if (null != this.rigidbody)
{
//Syndicate the mousedown event
if (null != this.transform.GetComponentInChildren<LiveEditObjectRotate>())
{
this.transform.GetComponentInChildren<LiveEditObjectRotate>().OnMouseDown();
}
}
Here you will see that i am checking to see if there is a rigidbody, then I pass the event down to my script.
This is the only way I’ve found this to work if your parent object has a rigidbody on it.
Thanks Eric for providing the correct information.
I know this is an old thread, but I just solved this problem by adding another rigid body to the child that is hosting my OnMouseDown script. This disassociated the parent rigid body and now the OnMouseDown event works as expected.