- Home /
On Trigger Enter Not Executing?
Edit Thanks to the user KillerStarBunny I was able to figure out my problem, I just needed to add a rigid body to my "lightCollider" object and I was good to go. Thanks.
Hello, I'm having an issue involving the "OnTriggerEnter" command. I have this code:
function OnTriggerEnter(collider : Collider)
{
if(collider.tag == "lightCollider")
{
isAlerted = true;
}
}
This code is inside of a script attached to an object with a capsule collider with "IsTrigger" set to false. The tag "lightCollider" is found on an object that has a capsule collider with "IsTrigger" set to true.
My problem is that when the lightCollider object moves into the collider for the object holding the script, isAlerted does not change from false to true.
If this might help, here is the entire script including the snippet mentioned above:
var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform;
var animation_crouchedIdle : String;
var animation_standUp : String;
var animation_moving : String;
var animation_attacking : String;
var you : Transform;
var thePlayer : Transform;
var dist : float = 10.0;
var isAlerted : boolean = false;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
for (var state : AnimationState in animation) {
state.speed = 0.75;
}
}
function OnTriggerEnter(collider : Collider)
{
if(collider.tag == "lightCollider")
{
isAlerted = true;
}
}
function Update()
{
if (isAlerted == true){
GetComponent(NavMeshAgent).destination = target.position;
transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));
var distanceToDestroy = Vector3.Distance(thePlayer.position,you.position);
if (dist > distanceToDestroy){
animation.CrossFade(animation_attacking);
}
if (dist < distanceToDestroy){
animation.CrossFade(animation_moving);
}
}
if (isAlerted == false){
animation.CrossFade(animation_crouchedIdle);
}
}
Please post actual answers and not just links to a webpage that might hold the answer. Thank you.
I guess the first question is does the object definitely have the tag "lightCollider"?
Also, could you comment on how you are testing the Trigger as I was unable to get it to trigger when moving the object with the script into the "lightCollider" via the scene view. (using 4.5.0f6).
As with every question on this site about triggers apparently not firing, first thing to confirm is that you do have a rigid body attached to the object?
It does have the tag lightCollider. @Ben Stoneman that is the same problem I am having, it just isn't triggering. That's what I'm asking. Also, I do not have a rigid body attached to the object.
Answer by tokyomage · Jun 17, 2014 at 03:21 AM
I figured it out! I needed to add a rigid body to my "lightCollider" object in order for it to execute the trigger object, thank you so much @KillerStarBunny .
Your answer
![](https://koobas.hobune.stream/wayback/20220613151713im_/https://answers.unity.com/themes/thub/images/avi.jpg)