Unity 2D Trigger Detection is not working.

Hello All,

I am currently trying to get a 2d sprite character moving around on screen and able to interact with the game world. I intend to do this with triggers. The character is a 2d sprite with a sprite renderer and animator setup. It also has a 2d box collider attached and is currently tagged player. I also have a rigidbody assigned to it.

To test out the ability of interacting with the world, I setup a simple 2d box collider with the trigger checkbox marked and called the game object the ‘Interaction area’. The idea is that my fully functional character walks into the area and a print statement prints to the console. this is the code that does it (and is attached to the game object i call floor trigger, and also assigned a rigidbody to it):

using UnityEngine;
using System.Collections;

public class Floor_Trigger : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void OnTriggerEnter(Collider other){
		Debug.Log("Something has entered this zone.");	
	} 
	void OnTriggerExit(Collider other){
		Debug.Log("Something has entered this zone.");	
	} 
	void OnTriggerStay(Collider other){
		Debug.Log("Something has entered this zone.");	
	} 

	void OnCollisionEnter(Collision collision){
		Debug.Log("something has hit me");
	} 
}

I put in various trigger methods in case any one of them were fired. But so far, absolutely zero luck. And I have looked around on the internet and google (please do not post that I should search google), and so far the methods I have tried out haven’t gotten this to work. Is there anything that I am missing?

I found out the issue. The reason it’s not working is because I wasn’t supposed to use Collider in the first place.

After a few more minutes of investigating, I found an API link to the Collider2D Class: Unity - Scripting API: Collider2D

From there I found my answer: the OnTriggerEnter2D() method. With this, unity worked exactly the way I expected. Will admit, I had no idea that the team made an entirely separate class for 2D based calculations.

Try this

Void OnTriggerEnter2D(Collider2D other)
{

}

Void OnTriggerExit2D(Collider2D other)
{

}

Void OnTriggerStay2D(Collider2D other)
{

}

Hope this help

I know what problem u met.
Fix :
Right-Click in Project(TAB)
→ Reimport → and try again.

Good Luck

Solved!!!

I faced the same problem but solved it by myself with a simple trick!
Follow the steps :

  1. Keep your object\image in moving continuously to use “OnTriggerEnter2D” function perfectly !
    Suppose your object\image is remain still in a certain position.
    now get the X(horizontal) value of your image\object from “Start” function like below :

  2. Now create a function to increase this X(horizontal) value continuously like below :

  3. Now call the fucntion from the "Update () " function :

    var temp : float = 0;

    function Start () {

                temp = Image1.position.x;
                
    }
    
    function KeepMoving()
        {
           Image1.position.x = temp + 0.00001; // increase the x value slightly!
           Image1.position.x = temp; // again move the fast position
        }
    
    function Update () {
        
          keepMoving();
        
         }
    

Hi, for Unity 5.5.0f3, if body Type in rigidbody component as Static it’s not work, but if your chose another type it’s work.

where tf is crohr’s comment? I get e-mails about comments on these forums that take me to the top of the forum when I click them. Also, wtf is with this autocorrect on these pages…Dear Unity, your improvements are great with the software…I’m so glad you released 5.1 and fixed soo many things…Please fix these couple issues as well…