Solution to double coin pickup

Hi all, I had the double coin pickup issue and also the issue where the sound was distorted when using Rick’s method. I used code to disable the collider and sprite renderer while the sound played which solved both problems at the same time. Enjoy!

 if(other.tag == "Player")
        {
            Destroy(gameObject, 1f);
            coinPickupSFX = GetComponent<AudioSource>();
            coinPickupSFX.Play();
            coinSprite = GetComponent<SpriteRenderer>();
            coinSprite.enabled = false;
            coinCollider = GetComponent<CircleCollider2D>();
            coinCollider.enabled = false;
}

So this was me being dumb and not a solution at all. Bool was still required to make sure there are no double pickups.

The main problem is that there are two colliders on the player and when you hit the coin first with your collider for the feed you have a duplicate OnTriggerEnter2D event and the script adds the coin value twice.

The solution for me was to add this code just on top of the OnTriggerEnter2D().

if(other.isTrigger){
return;
}

2 Likes

Hello,

Can you please explain this because I tried it and it worked but none of th e colliders is a trigger.

Thank you in advance

Hi @StrawHat,
the coin itself is the trigger and does not need any other :wink:

Example:
Feet collider touches the coin = activates the trigger of the coin
Body Collider touches the coin = activates the trigger of the coin

and so the value of the coin is counted twice

Yes I know that the coin is a trigger. The event OnTriggerEnter2D is on the coin.

So, when you type other.isTrigger it checks the colliders of the player’s collider components; the other object is the player in this case. Correct? But none of them is a trigger.

I don’t understand it.

The only logical explanation is that once the first collider (feet) on the player triggers the coin collider then the feet collider also sets the player’s object property “isTrigger” into true. Hence, when the coin collides with the second collider reads the isTrigger of the player as true and exits the method

Update: tested it again and it does not work (on my game at least.). It still counts some coins as 200 even though the value is 100 for each.

Well said and done that example of yours, I have the exact issues, added a bool but still got double points for coins and other similar pickups…
I thought that right when the player’s feet collider touched the coin, the coin itself got deactivated through gameObject.SetActive(false) right? Then why the player’s body collider can still trigger the coin?

Privacy & Terms