Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

OnTriggerEnter2D not working

Discussion in '2D' started by Cray.x, Mar 10, 2014.

  1. Cray.x

    Cray.x

    Joined:
    Mar 10, 2014
    Posts:
    4
    i'm trying to learn and understand the working of 2D collider detection so i was making a project based on the tower defense assets of the unity 2D projects, but i'm having a problem i used the 'bomb crate' sprite and placed it in the space and its fixed so that my character can hit it just like mario, but whenever it hits it the OnTriggerEnter2D method is not called
    here is the code i attached the code both to the bomb crate and the character
    the bombcrate has a 2D box collider and rigidbody2d components
    and the character has 2D circle and box collider
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class colliderdetect : MonoBehaviour {
    5.     public int number=0;
    6.  
    7.     void OnTriggerEnter2D(Collider2D coll)
    8.     {
    9.         Debug.Log("is in the trigger");
    10.         if(coll.tag=="Player")
    11.         {number++;
    12.             Debug.Log(number);
    13.     }
    14. }
    15. }
    16.  
    please help me i dont know what to do and i'm a newbie.
     
  2. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    One of the objects must have a rigidbody component and should be a trigger.
     
    dziastiz likes this.
  3. Cray.x

    Cray.x

    Joined:
    Mar 10, 2014
    Posts:
    4
    thanks a lot Bruno! it worked!!!! :)
     
  4. FelipeStenio

    FelipeStenio

    Joined:
    May 14, 2013
    Posts:
    1
    I'm still with the problem, even doing what you recommended ...

    by searching, discovering that it was a bug in own version of unity, but even upgrading the problem remains ...

    the project's link is attached, who knows the solution thank you very much


    link: http://www.sendspace.com/file/9ii65m
     
  5. Badams66

    Badams66

    Joined:
    Mar 11, 2014
    Posts:
    49
    Took a quick look at your project. Attach your "playerControl" script to your character instead of in an empty game object.
    Then it should work fine

    Right now youre running "OnTriggerEnter2D" on an empty game object instead of on your character.
     
  6. howartthou

    howartthou

    Joined:
    Mar 5, 2017
    Posts:
    1
    In my case I needed to check the "Simulated" checkbox on the Rigidbody2D. If this isn't checked the physics engine basically ignores the rigid body (as I understand it). Simulated is meant to be on by default so the physics engine won't ignore it.
     
    SethJMoore, fuzzzzel and mateo_gon like this.
  7. fuzzzzel

    fuzzzzel

    Joined:
    Jul 28, 2020
    Posts:
    1
    @howartthou That checkbox solved it for me, thank you very much!
     
unityunity