Double Pickups Problem

So I still have that double pickups problem even after added a bool, not just with coins but with any other similar pickups like health, gems, etc…
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?

Here is my code:

void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("Player") && isCollected == false)
    {
        gameSession.ProcessPlayerScore();

        isCollected = true;

        // chạy audio
        coinAudioSource.PlayOneShot(coinPickupSound, volume);
        // PlayClipAtPoint sẽ không bị mất đi khi huỷ gameObject 

        // tắt đi để ngăn kích hoạt pickups nhiều lần
        gameObject.SetActive(false);

        // tạo hiệu ứng bay lên khi chạm vào đồng xu
        coinRigidbody.velocity = coinVelocity;

        // sau 2s thì destroy gameObject
        Invoke("KillObject", destroyDelay);
    }
}

Well, i have tried both ways with the coin gameObject,
gameObject.SetActive(false); or with coinCircleCollider.enabled = false;
Nothing helped, still got the bug here and there, my guess is that bc my player had more than one collider on it so when the player touched the coin, it colliders just activated the Trigger method faster than the coin deactivating itself, knowing that but all I could is stopping the pickup sound from playing twice, the score still double up sometimes. :sob:

Hi,

I’m afraid I cannot see that anywhere in the code you shared but gameObject.SetActive(false) is usually a good idea to prevent other game objects to interact with this object. If you call this method with a delay, the game object will be active until gameObject.SetActive(false) was called. Destroy destroys at the the of a frame. This means: Until the end of the frame, an active object can still be found.


See also:

Well thanks, now i understand the problem and still not knowing how to solved it :frowning:

i fixed it using this method, dont know if it’s the best or optimized yet, still works like a charm and that’s all i need, to all having the same prob like me even after adding bool, add another condition in just to ignore all others player collider:

Thanks for the assist! :grinning_face_with_smiling_eyes:

That’s interesting. I wouldn’t have expected that ‘is CapsuleCollider2D’ would have solved the problem. Good job on figuring that out, and thanks for sharing! :slight_smile:

1 Like

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

Privacy & Terms