Get Collider of Child Object?
Hello, i cant manager to get the collider of a child object.
I basicly want to spawn an objcet on top of me and Debug when it hits something else then me. My Player itself has a collider with the "istTigger" box unchecked and it has a child object, which itself has a collider component with the "isTrigger" box checked.
The following Code is in the object i want to spawn. While spawning it immediately calls the Debug Message with the player, and the child object of the player. However i dont want any of them to trigger, how could i do this?
Answer by Hellium · Dec 05, 2017 at 12:00 PM
You will have to gather by yourself the children colliders. Try this :
private List<Collider> childrenColliders ;
private void Start()
{
childrenColliders = new List<Collider>();
AddChildrenColliders( transform );
}
private void AddChildrenColliders(Transform t)
{
for( int i = 0 ; i < t.childCount ; ++i )
{
Transform child = t.GetChild(i);
AddChildrenColliders( child ) ;
Collider c = child.gameObject.GetComponent<Collider>();
if( c != null )
childrenColliders.Add( c ) ;
}
}
private void OnTriggerEnter( Collider col )
{
if( IsChildCollider(col) )
return ;
if( ... )
{
// your code
}
}
private bool IsChildCollider( Collider col )
{
for( int i = 0 ; i < childrenColliders.Count ; ++i )
{
if( childrenColliders[i].GetInstanceID() == col.GetInstanceID() )
return true ;
}
return false;
}
Your answer
Follow this Question
Related Questions
How to set an object to be a child of another using c# 1 Answer
How To how ?? 2 Answers
Collision with object not working. 2 Answers
Help with Trigger Colliders.. 0 Answers