- Home /
Collision counter
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) {
Do you want me to make an "int collisionCount;"? Then what? How can I display the counter? @tanoshimi
Answer by Priyanshu · Mar 22, 2015 at 11:05 AM
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 ++;
}
Answer by Digital-Phantom · Mar 22, 2015 at 11:08 AM
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.
:)
One thing: I used OnGUI to show counted collisions. Can I use another way to show the numbers?