I have a object that consists of 3 different game objects, and its all under one empty object. I want to be able to detect OnMouseDown on all of the children through one script.
Ya I would agree with AlwaysSunny if the only other way to do this with one script would be to do a raycast from the mouse click and check if the object clicked on is a child of your object. What I would suggest though is taking Always Sunny’s idea but to only have to put one script on. The way you do this is at Awake you can have your parent script go through all of its children and add the notifying component. An example might be something like this
public class ParentObj:MonoBehaviour
{
void Awake()
{
for(int i =0;i<tranform.childCount;i++)
{
GameObject g = tranform.GetChild(i).gameObject;
if(g.GetComponent<ChildObj>()==null)
{
g.AddComponent<ChildObj>().parent = this;
}
}
}
public void OnChildClick()
{
//Do what ever
}
}
public class ChildObj:Monobehaviour
{
public ParentObj parent;
void OnMouseDown()
{
parent.OnChildClick();
}
}
There might be some errors as I wrote it all here but hopefully that gets the idea of it
Couldn’t you just add a static rigidbody to the parent? That way it combines all of it’s child colliders, and OnMouseDown() and the like will be called.