Trigger Enter and Exit not working properly?

Update: I have tried doing it all over again, this is still happening. Any other ideas?

Hello,

As an awful way to double check for a collision, I’ve created 4 colliders(divided in 4 directions) attached to an empty gameobject.

Each of the object turns a boolean from the empty gameobject script - true with OnTriggerEnter and false OnTriggerExit.

When all of them are true something (not sure what yet) is going to happen.

The problem is the following: while my AI is moving (so are the colliders) and if they move quickly they cancel each other somehow that they are unable to be all true although all of them are colliding with the triggers.

Here is an example of the code I use on one of the colliders:

function OnTriggerEnter(hit:Collider)
{
	if(hit.collider.tag == "Fence")
	{
		var gameObject = GameObject.FindGameObjectWithTag("DoubleCheck");
		var script = gameObject.GetComponent(DoubleCheckAI);
		script.topDcheck = true;
	}	
}

function OnTriggerExit(hit:Collider)
{
	var gameObject = GameObject.FindGameObjectWithTag("DoubleCheck");
	var script = gameObject.GetComponent(DoubleCheckAI);
	script.topDcheck = false;
}

Notes: I have kinematic rigidbodies attached on the colliders. I have the colliders set to isTrigger.

Any help on this would be greatly appreciated because I’ve never encountered this until now…

EDIT (Scenario information): Certainly.

My AI is moving constantly (currently the AI is a sphere with a sphere collider).

Attached to my AI I have an empty game object with a cube collider to count the number of fences. Also attached to the AI I am doing a double check of the collision using 4 box colliders (for 4 directions) - these are the problem.

The fences are prefabs instantiated on click + drag in a certain direction.

The idea of the game is to capture the AI between 4 fences - that is what isn’t working for me at all times.

Due to the issue with the double check I am not always seeing that the AI is captured if it moves. If my AI would be static if wouldn’t be a problem as I’ve tried moving the colliders manually while pausing the unity player.

Not sure what to do next…

looks like you’re overriding gameObject with var gameObject. rename it to var gObject and try again. gameObject is a reserved name you shouldnt override it.

try

function OnTriggerEnter(hit:Collider)
{
    if(hit.collider.tag == "Fence")
    {
       var gObject =  GameObject.FindGameObjectsWithTag("DoubleCheck");
       if (gObject==null) return;
       for (var n in gObject)
           {
           var script = n.GetComponent(DoubleCheckAI);
           script.topDcheck = true;
           }
    }  
}
 
function OnTriggerExit(hit:Collider)
{
    var gObject =  GameObject.FindGameObjectsWithTag("DoubleCheck");
       if (gObject==null) return;
       for (var n in gObject)
          {
          var script = n.GetComponent(DoubleCheckAI);
          script.topDcheck = false;
          }
}

Two ideas:

1) Any chance you’ve mixed up the 3D and 2D version of these methods and components? I do that all the time.

  • Rigidbody Vs Rigidbody2D
  • Collider.OnTriggerEnter(Collider) Vs Collider2D.OnTriggerEnter2D(Collider2D)
  • Collider.OnCollisionEnter(Collision) Vs Collider2D.OnCollisionEnter2D(Collision2D)
  • BoxCollider Vs BoxCollider2D

etc.

2) Another thing that happens is you have the OnCollisionEnter2D setup in code, but you set the BoxCollider2D to be a Trigger, so you should be using the OnTriggerEnter2D in code.