Game over on collision

Hello !

Well I have a problem, my game is a 2D runner and I want that if my player touch an obstacle he loose

My obstacles are tagged “Enemy” and have a box collider.
My player is not tagged but have a box collider.

This is my script (attached into the player)

void OnCollisionEnter(Collision whatHitMe)
    {
        if(whatHitMe.gameObject.tag == "Enemy")
        {
            Debug.Log("You loose");
Gameover();
        }
    }

But when my player touch an obstacle “You loose” is not written in the console do you know why ?

Thanks for your help !

Make sure they have Rigidbodies?

2 Likes

to expound on that

no rigidbody involved, physics system doesn’t know to look for a collision

Make sure both the character and the other object both have “Is trigger” selected in their respective collider components.

You’re also making the code a big harder on yourself, try:

void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Enemy"))
{
Debug.Log("You lose");
}}

Sorry my bad, my player and my enemy are not “Istrigger”
Well the problem is that when I click on “Is trigger” for my player and my enemy they fall even if there is a box collider above them (nothing collide).

When they are not “Is trigger” :
2234738--148943--QUESTIONS.png
When they are “IsTrigger” :
2234738--148944--QUESTIONSé.png

If you just want it to collide with, then you would make sure the collider is turned on. So for the floor blocks underneath the character, just make sure the collider components are activated. For the enemies, make sure the colliders are activated, and the Is Trigger is also activated.

Let me know if that doesn’t help.

To expand on the other answers here, I notice that you’re using 2D graphics and refer to it as a 2D game, but you are using the unity callers for 3D physics.

If you’re using regular Rigidbody and Box Collider components for the physics then the problem is elsewhere.

However, if you’re using Rigidbody2D and BoxCollider2D, you’ll want to use OnCollisionEnter2D(Collision2D whatHitMe);

1 Like

It works thanks guys !!!

2 Likes

How did u get it to run I’m having the same problem?