Search Unity

OnTriggerEnter2D not working

Discussion in '2D' started by TortoRacoon, Oct 3, 2019.

  1. TortoRacoon

    TortoRacoon

    Joined:
    Apr 17, 2019
    Posts:
    61
    Hello, I want to make a bullet prefab that deals damage to the enemy it hits. Pretty easy right?
    So I have my bullet with a script that has speed, damage a RigidBody2D and a Collider2D with the isTrigger checked.

    in the Script I wrotte OnTriggerEnter thing:


    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D col)
    2.     {
    3.         Debug.Log(col.gameObject.name + " : " + gameObject.name + " : " + Time.time);
    4.  
    5.     }
    but it-s not detecting anything. I did it many times, checked if anything was missing, watched that Brakeys tutorial to see what I was doing wrong. They did exactly the same as I, the only difference is that theirs work and mine doesnt HELP PLEASE!

    I really dont see it, WHAT IS WRONG!?
    upload_2019-10-3_12-53-35.png
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    How did you set up your enemy objects that should be detected?

    These are the requirements for a 2D trigger event:
    • Both objects have a Collider2D, and the one you are coding for is set to "IsTrigger"
    • At least one has a Rigidbody2D that is simulating
    • Both GameObjects are on layers that are configured to collide in the layer matrix in the Physics2D project settings
    Then to catch the event, you define OnTriggerEnter2D, which you have done. It will only be called within a class that inherits from MonoBehaviour, and only if that script is attached to the same GameObject as the Collider2D involved in the collision.

    Hope that helps.