When touching an enemy, it shows clones twice instead of once.

Hello there Unity Experts!

I need some help with my coding. When the player touches the enemy, it get’s destroyed but it clones twice instead of once (trying to make a Super Mario Bros clone). Can someone help me with this? Here’s the Enemy Damage code.

using UnityEngine;
using System.Collections;

public class EnemyDamage : MonoBehaviour
{
    public GameObject ShellPrefab;

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Player")
        {
            switch (PlayerController.PlayerState)
            {
                case 1:
                    PlayerController.PlayerState = 0;
                    break;
                case 2:
                    PlayerController.PlayerState = 1;
                    break;
                default:
                    Destroy(col.gameObject);
                    break;
            }
        }
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "Player")
        {
            Destroy(this.gameObject);
            Object Shell = Instantiate(ShellPrefab, transform.position, transform.rotation);
        }
    }

}

If your code seems normal the most likely cause of this annoyance is in the hierarchy, it’s not really mentioned as much as it should be in tutorials etc. but if you have two colliders and only one rigidbody then Unity will by default seem to grab the first object in the hierarchy. If you have a different gameobject with an extra collider then it needs a rigidbody attached to it in order for the engine to treat the collisions separately.

Let me know if that works for you, also be sure to double check if you haven’t tagged any children with separate colliders and triggers by accident with the Player string. You also don’t need to use this as it’s deprecated, just use gameObject.

Alright. Testing it rn.

I do have 2 box collider 2d’s. Do I remove one of them or?

If you need the second box collider then simply add a rigidbody component to the same gameobject it’s attached to it should then all work correctly. Adjust your rigidbody settings to match the other if you want them to behave the same.