How can I get a counter that counts the collisions in my 2D game? I use this code to register the collisions:
void OnCollisionEnter2D(Collision2D collision)
{
How can I get a counter that counts the collisions in my 2D game? I use this code to register the collisions:
void OnCollisionEnter2D(Collision2D collision)
{
Considering all the requirements for Collision to occur are fulfilled. Whenever an Object collides with the Object (with the script), OnCollisionEnter event will be called.
To get a counter that counts the time OnCollisionEnter is called.
Just put a variable inside OnCollisionEnter and increase its value by 1.
For example:
void OnCollisionEnter2D(Collision2D collision)
{
counter ++;
}
You basically said it yourself in the comment.
make an int variable (collisionCount) then in your collision code just add
void OnCollisionEnter(Collision2D collision)
{
//Do whatever else you need to do here
collisionCount += 1;
}
You could also add a debug or print function if you actually want to see the number of collisions as they occur.