How do I make OnTriggerEnter() respond to just one collider?

Hi guys,

I have some checkpoints in my game, and I’ve decided to use OnTriggerEnter() for them. Here is the implementation in Checkpoint.js:

`
function OnTriggerEnter (other : Collider) {

	if (other == engine.collider);
	levelManager.SendMessage("RegisterCheckpointHit");
}
`

levelManager is set to a shared instance of the prefab LevelStateManager, and it has a script attached called LevelStatus.js. The implementation of RegisterCheckpointHit is as follows:

`
function RegisterCheckpointHit()
{
if (checkpointsHit < numCheckpoints) checkpointsHit++;
Debug.Log("Checkpoint Hit");
}
`

The RegisterCheckpointHit() function works exactly as expected, however OnTriggerEnter gets called multiple times because the player object has multiple colliders. The player object consists of a character with multiple colliders, as well as an engine with just one collider. Because there are multiple colliders, the collider for the checkpoint gets triggered multiple times. The attempt to restrict it to the engine’s collider with the following code:

`
if (other == engine.collider);
`

Doesn’t seem to be working. What am I doing wrong?

MachCUBED

Take a look at Layer-Based Collision Detection.

What you can do is having a child object on your player with a collider that is in the only layer that collides with the layer of your triggers.

remove the semicolon in your if (other == engine.collider) line

to avoid all sorts of errors with if’s in future, make use of curly braces for if statements.

if (other == engine.collider) {
      levelManager.SendMessage("RegisterCheckpointHit");
}