Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Game over on collision

Discussion in 'Scripting' started by simonini_thomas, Aug 3, 2015.

  1. simonini_thomas

    simonini_thomas

    Joined:
    Apr 30, 2015
    Posts:
    33
    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)

    Code (CSharp):
    1.  
    2. void OnCollisionEnter(Collision whatHitMe)
    3.     {
    4.         if(whatHitMe.gameObject.tag == "Enemy")
    5.         {
    6.             Debug.Log("You loose");
    7. Gameover();
    8.         }
    9.     }
    10.  
    But when my player touch an obstacle "You loose" is not written in the console do you know why ?

    Thanks for your help !
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Make sure they have Rigidbodies?
     
    LeftyRighty and GroZZleR like this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. Gerald Tyler

    Gerald Tyler

    Joined:
    Jul 10, 2015
    Posts:
    80
    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:

    Code (csharp):
    1.  
    2. void OnTriggerEnter(Collider other)
    3. {
    4. if (other.gameObject.CompareTag ("Enemy"))
    5. {
    6. Debug.Log("You lose");
    7. }}
    8.  
     
    Last edited: Aug 4, 2015
  5. simonini_thomas

    simonini_thomas

    Joined:
    Apr 30, 2015
    Posts:
    33
    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" :
    QUESTIONS.png
    When they are "IsTrigger" :
    QUESTIONSé.png
     
  6. Gerald Tyler

    Gerald Tyler

    Joined:
    Jul 10, 2015
    Posts:
    80
    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.
     
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    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);
     
    neodev likes this.
  8. simonini_thomas

    simonini_thomas

    Joined:
    Apr 30, 2015
    Posts:
    33
    It works thanks guys !!!
     
    TaJeiii and GarthSmith like this.
  9. TaJeiii

    TaJeiii

    Joined:
    Dec 1, 2019
    Posts:
    1
    How did u get it to run I'm having the same problem?