Bool wasCollected to avoid Score bug

Hello, just a quick question about the nescessity of having a bool wasCollected so that we can ensure that the coin cannot be picked up twice.

Does the Bug happen because we have two colliders on the player character and in cases where they both collide with the coin it is counted twice?

In other words, any collider component we apply to the character is tagged with the tag that we assign to the parent and this is why the problem occurs?

Thank you in advance.

Hi StrawHat,

That might well be the case. Destroy() does not destroy the object immediately but at the end of the frame. Until then, everything could still find the object and interact with it unless we disable the Collider2D component or simply the entire game object. Also, Unity’s execution order of its own methods is random, which often causes problems if our own logic rely on a specific execution order.

A bool is a good solution to ensure that Unity’s inner mechanics don’t contradict our logic. Since we cannot change Unity, we usually try to find solutions we could implement in our own code.

Is this what you wanted to know?


See also:

Thank you very much Nina!!

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

Privacy & Terms