- Home /
OnTriggerEnter not detecting tag
I have this code for turning on and off certain scripts upon trigger enter but instead of it only working on trigger enter it runs through the script without a trigger entry. I believe it's not detecting that I am asking for it to look for a tag. Any idea's why? (Unity4 Question, Javascript)
#pragma strict
function Start(){
}
function OnTriggerEnter (trigger:Collider)
{
if(trigger.gameObject.tag=="Player");
{
(GameObject.FindWithTag("Player").GetComponent( "show_hidecursor_lost_forest") as MonoBehaviour).enabled = false;
(GameObject.FindWithTag("Player").GetComponent( "simplepause_lost_forest" ) as MonoBehaviour).enabled = false;
}
}
function OnTriggerExit (trigger:Collider)
{
if(trigger.gameObject.tag=="Player");
{
(GameObject.FindWithTag("Player").GetComponent( "show_hidecursor_lost_forest") as MonoBehaviour).enabled = true;
(GameObject.FindWithTag("Player").GetComponent( "simplepause_lost_forest" ) as MonoBehaviour).enabled = true;
}
}
it should work, have your player collider the that tag? An usual mistake is when someone create a child with the player collider and then he don't tag it like Player.
Yes the player is tagged correctly and is the only object tagged "Player"
Answer by Michael_-01-_ · Apr 16, 2015 at 09:11 AM
Try to use this :)
function OnTriggerEnter(trigger:Collider)
{
if(trigger.collider.gameObject.tag == "Player")
{
//Your code here
}
}
Answer by KalleH · Apr 16, 2015 at 01:59 PM
Hey! The problem is that you have a semicolon after your if-statements on lines 11 and 24. Removing those should make the code work properly.
That worked perfectly, thanks!! $$anonymous$$ind explaining why the semicolons were not necessary in this case? :)
Semicolons end statements. if() is not a statement - it's a condition. Semicolons never end conditions.