- Home /
Trying to deactivate a script when I'm not in the collider area
I want to deactivate the script when I'm not on the area of the collider, when I enter to the area, it activate and that's ok but when I leave the area still are activate. This is my script:
public GameObject car;
autoTeletransport script;
void Start ()
{
script = car.GetComponent<autoTeletransport> ();
script.enabled = false;
}
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "Player") //When I enter in the collider area
{
if (script.enabled == false)
{
script.enabled = true;
}
}
else if (col.gameObject.tag != "Player") //When I leave the collider area but the script didn't stop it
{
if (script.enabled == true)
{
script.enabled = false;
}
}
}
}
Sorry for my bad english. Thanks
Answer by domportera · Dec 15, 2017 at 11:35 PM
Hey I see your reasoning here, but it's important to note that OnTriggerEnter only fires when the trigger is first entered by an object. Therefore, it runs once and it's done.
Fortunately, there's a handy OnTriggerExit function.
void OnTriggerExit (Collider col)
{
if (col.gameObject.tag == "Player") //When I enter in the collider area
{
if (script.enabled == true)
{
script.enabled = false;
}
}
Hope this helps! :)
THAN$$anonymous$$ YOU SO $$anonymous$$UCH, IT WOR$$anonymous$$S
Your answer
Follow this Question
Related Questions
FPS walking down stairs cant walk back up again 0 Answers
Making a bullet invinceble 0 Answers
Script not recognizing collision 3 Answers
the object passes through the collider 1 Answer
Raycast shooting problems 1 Answer