Detecting collision on child with compareTag do not work

Detecting collision on child with compareTag do not work, it is always detected on the parent gameobject. What is wrong?

My script is attached on the player shooting bullet object:

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.transform.CompareTag("EnemyChild"))
        {
            Debug.Log("CHILD " + collision.gameObject.name);
            hit = true;
        }
        else if (collision.gameObject.transform.CompareTag("Enemy"))
        {
            Debug.Log("PARENT " + collision.gameObject.name);
            hit = true;
        }
    }

If you need to know if you hit the child’s collider or the parent’s collider then you can go through the collider reference:

	void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("EnemyChild"))
        {
            Debug.Log("CHILD " + collision.collider.name);
            hit = true;
        }
        else if (collision.collider.CompareTag("Enemy"))
        {
            Debug.Log("PARENT " + collision.collider.name);
            hit = true;
        }
    }

Or if you just want to do damage to the enemy regardless of which collider you hit:

	void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            Debug.Log(collision.gameObject.name);
            hit = true;
        }
    }
1 Like

Thank you. if (collision.collider.CompareTag(“EnemyChild”)) worked.

Another problem occurs.

if I use Destroy(this.gameObject); in a script attached on the child object the whole parent gameObject will get destroyed. How can I destroy only the child gameobject?

What may be happening is that you also have the same script on the parent object and that is also detecting the collision of the child object and so both the parent and child are being destroyed. You can make it so that only the gameobject with the collider that was involved in the collision destroys itself.

	void OnCollisionEnter2D(Collision2D c)
	{
		if (c.otherCollider.gameObject==gameObject) // are we the collider that was involved in the collision?
			Destroy(gameObject);
	}

I tried in the bullet script where the collision is detected to set the childGamobject into the enemybase script

enemyChildHealthScript.enemyBaseScript.gameObjectThisChild = collision.collider.gameObject;

in the enemyBaseScript I have this code:

                    if (isChild) // && gameObjectThisChild != null)
                    {
                        Debug.Log("gameObjectThisChild " + gameObjectThisChild.name);
                        Destroy(gameObjectThisChild.gameObject, soundExplosion.length);
                        //gameObjectThisChild.gameObject.SetActive(false);
                    }

The Debug-Log write the correct name of the child gameobject but if I destroy the childgameobject the parent will get destroyed too, even SetActive=false disable the parent and not only the child. I don’t understand why the parent will get destroyed because the Debug.Log write the correct gameObject-name of the child.

The enemybasescript is attached to the parent and to the child.

I found my bug, I deleted the root gameObject in a OnBecameInvisible() function.

1 Like