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. Dismiss Notice

OnCollisionEnter2D not working properly !

Discussion in '2D' started by Loaiq1107, Jun 3, 2016.

  1. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    So i've been working on my latest 2D game , and I've done it that if a rocket hits my player (which is a planet) , both of them blow up... BUT i'm trying to make it that two rockets blow up if they hit eachother as well , but that's not working , only if it's a player vs a rocket !

    The both rockets are the SAME , which means same tags , same kind of collider (Polygon 2D) , same components , etc... i tried to change the tag or add an another 2D Collider , but none of that worked :/

    Here's my script :
    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D other)
    2.     {
    3.         if (other.gameObject.tag == "Player") {
    4.             ((MonoBehaviour)gameObject.GetComponent ("FireBase")).enabled = true; //Blowing up effect
    5.             KillPlayer ();
    6.             StopRocket = true;
    7.             StopJoystick = true;
    8.            
    9.             gameObject.GetComponentInParent<SpriteRenderer> ().enabled = false;
    10.         }
    11.    
    12.         if (other.gameObject.tag == "Rocket") {
    13.             Debug.LogError("+1 Point");
    14.             ((MonoBehaviour)gameObject.GetComponent ("FireBase")).enabled = true; //Blowing up effect
    15.  
    16.         }
    17.     }
    Any help is very appreciated !!
    Thanks :)
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    So which part is not working? Does the OnCollisionEnter2D get called when it's a Rocket-Rocket collision? Does your debug statement get called?

    By the way, you don't need to write "gameObject.GetComponent", just "GetComponent" works fine.

    Also, you can use the same syntax for the GetComponent<SpriteRenderer>() as for GetComponent<FireBase>().
     
  3. poler300

    poler300

    Joined:
    Nov 3, 2015
    Posts:
    16
    You may need a box collider ;)
     
  4. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    Bro i tried.
    And logically it should work on any other collider as well , it's a collider after all !
     
  5. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    The Rocket-Rocket Part is not working , this script is attached to the rockets , and no the debug statement doesnt get called :/ ....

    Um i think i do need to write gameObject.GetComponent here Because it has the (MonoBehavior) before it. in other cases i know you can write GetComponent<> alone :) .

    p.s : i'm writing (MonoBehavior) before the GetComponents because it's not finding the components i want if i write it regularly , so this is an another way , and worked !
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Do the rockets have rigid bodies attached?
     
    Loaiq1107 likes this.
  7. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    No... only the player has one

    I just tried adding rigidbodies to the rockets and it WORKED!!!
    Thanks a lot man really appreciate it!
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    No problem. I'm not 100% certain but I'm fairly sure that at least one object in a collision/trigger event has to have a rigid body, even if it iskinematic.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,372
    The problem is that a collider with a Rigidbody2D is a static collider and should not be moved. Static colliders don't contact each other. When you add a Rigidbody2D then it becomes a Dynamic/Kinematic body.

    If you look at the 2D experimental preview reference guide, I explain the body-type in more detail: http://forum.unity3d.com/threads/2d-experimental-preview-release-1.407658/
     
    manelizzard and tedthebug like this.
  10. Loaiq1107

    Loaiq1107

    Joined:
    Aug 25, 2014
    Posts:
    48
    Thanks a lot !
    I got it :D