- Home /
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!
There is no need for 2 different tags since you do the same thing, just tag both as Enemy Also, what is enemy = trigger.gameObject; supposed to do?
var enemy : GameObject;
function OnTriggerEnter(trigger : Collider){
if (trigger.gameObject.tag ==("Enemy") {
// do something here
}
}
You don't write "solved" in the title, you accept the correct answer by clicking the checkbox. Also don't post comments as answers.
Answer by rutter · Apr 05, 2012 at 06:48 PM
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.
Answer by dklompmaker · Apr 05, 2012 at 07:07 PM
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.
Your answer
Follow this Question
Related Questions
Reaching a gameobject from its collider 3 Answers
Multiple Colliders Trigger problem 2 Answers
Collision, tags, and triggers 1 Answer
I need help with triggers 1 Answer
Moving objects using raycasting? 1 Answer