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);
}
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.