Bush is spawning 2 coins!

Every bush is spawning 2 coins. I’ve narrowed down the problem to their being 2 colliders on the bush, the trigger collider for destructible and the non-trigger capsule collider so you can’t walk through the bushes.
I’ve triple checked and only the circle collider is a trigger, the other collider is not. If I turn off the capsule collider (non-trigger collider) , I get only 1 coin but if I turn that back on and turn off the trigger collider, it still spawns a coin.
So both the trigger collider and the non-trigger collider are causing OnTriggerEnter2D to fire

    private void OnTriggerEnter2D(Collider2D other) {
        if(other.gameObject.GetComponent<DamageSource>())
        {
            if(TryGetComponent(out PickupSpawner spawner))
            {
                spawner.DropLoop();
            }
            Instantiate(destroyParticles, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
        
    }

I hadn’t noticed this happening (it’s still not in the course project), but here’s a simple fix to prevent this quasi race condition.

bool hasTriggered;
private void OnTriggerEnter2D(Collider2D other) {
        if(hasTriggered) return;
        if(other.gameObject.GetComponent<DamageSource>())
        {
            hasTriggered=true;
            if(TryGetComponent(out PickupSpawner spawner))
            {
                spawner.DropLoop();
            }
            Instantiate(destroyParticles, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
        
    }

I went back and rewatched the destructible lecture, it didn’t add that trigger circle collider, so unless that’s added somewhere else later, I must have hallucinated it or something haha. There are trigger colliders on all the damage sources so the target doesn’t need one

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms