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.