Quantcast
Viewing latest article 23
Browse Latest Browse All 70

OnCollisionEnter2D being called twice

I need to calculate a "hit" during every collision. To avoid counting a hit with the same object twice, I check for the Instance ID. The problem is that the collision seems to be happening twice, even with my check. I tried to print on the Debug more information about the collision with the Instance ID and the time (*DateTime.Now*), and I can see that it is printing twice. Here is my code: void OnCollisionEnter2D(Collision2D coll) { Debug.Log("Bateu: " + lastCollisionID.ToString() + " - " + System.DateTime.Now); int gameObjectID = coll.gameObject.GetInstanceID(); if ((lastCollisionID != gameObjectID) && (coll.gameObject.tag == "tile")){ Debug.Log("Contou - " + System.DateTime.Now); levelScript.addHit(1); lastCollisionID = gameObjectID; } } And here is my Debug: ![alt text][1] [1]: /storage/temp/126489-unity-debug.png

Viewing latest article 23
Browse Latest Browse All 70

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>