- Home /
Trigger Enter Issue
I have a trigger enter that is working bizarrely. The trigger enter is looking to see if the player has entered it and if it does turn a game object on and then off on exit. However, when i start the game it automatically thinks that the player has entered the trigger despite the player being really far away from the trigger. I am good with Unity and scripting, used trigger enter for a lot of things without issue. Here is my code, if anyone can shed some light on this problem that'd be brilliant.
#pragma strict
var securityCameraHolder : GameObject;
var soundActive : boolean = false;
function Start () {
soundActive = false;
}
function OnTriggerEnter (playerEnter : Collider) {
if(playerEnter.gameObject.name == "player");
soundActive = true;
Debug.Log("TriggerEntered");
}
function OnTriggerExit (playerExit : Collider) {
if(playerExit.gameObject.name == "player");
soundActive = false;
Debug.Log("TriggerExit");
}
function Update () {
if(soundActive == true) {
securityCameraHolder.active = true;
Debug.Log("Security Camera is true");
}
if(soundActive == false) {
securityCameraHolder.active = false;
Debug.Log("security camera is false");
}
}
I could not find anything wrong with your code, But try using OnTriggerStay ins$$anonymous$$d of OnTriggerEnter and try tagging the objects ins$$anonymous$$d of calling them by their name, maybe it works that way.
need more info on the console outputs you are getting.
Also I notice you have a ; after the condition of your if statement in the trigger functions i.e.
if(playerExit.gameObject.name == "player"); <== ???? remove ;
also , try adding more info to the trigger debugs just to check if it is the playerObject that is triggering the event , i.e.
Debug.Log("TriggerEntered by " + playerEnter.gameObject.name);
Answer by venkspower · May 02, 2012 at 05:23 PM
Your script is absolutely right. Since you have initialized the value of soundActive to false, and have given if(soundActive == false)
in the Update, there will be printing "security camera is false" every frame. That is causing the problem.