Trigger can't see the enemy

Hi everybody

I’ve made a script I’m planning to use for attacking the enemy. I tagged 1 kind of enemy with the tag Enemy and another one with the tag Enemy1. Those enemies also both have a Sphere Collider attached to them. I just want the script to store the variable of the enemy entering the trigger, but it doesn’t work.

var enemy : GameObject;


function OnTriggerEnter(trigger : Collider){
if (trigger.tag ==("Enemy") || trigger.tag == ("Enemy1")){
enemy = trigger.gameObject;
}
}

But it doesn’t work. Why?

Please help me!

Hello,

First make sure that one of the objects has a Rigidbody component added to it. (Preferably the player object). Try that and see if that works. Also, double check that the Sphere Collider has the “Is trigger” check box checked.

  • Do both objects have colliders attached?
  • Does at least one object have a rigidbody attached?
  • Is at least one of the colliders a trigger?

You might check if you’re getting a call to OnTriggerEnter() at all, with a quick debug statement:

Debug.Log("I was called!");

If you don’t see that log, you know your collision isn’t quite set up right.

If you do see that log, you know that either your script’s logic isn’t quite set up right, or that your objects aren’t configured in the way that your script expects.