- Home /
How do I make it so that when my player enters an area, a disabled script attached to my player becomes active?
Title asks all.
Answer by aldonaletto · Oct 06, 2013 at 02:25 AM
You must create a trigger volume and use the OnTriggerEnter event. For the trigger volume: create an empty object (menu Game Object/Create Empty) and add a box collider (menu Component/Physics/Box Collider), then adjust its dimensions, position, rotation etc. and set the Is Trigger check box. It's a good idea to identify the trigger by name or tag, so that only the right trigger does the desired effect - let's call it "Area1", for instance.
The OnTriggerEnter event is sent to both, the player and the trigger - the easiest solution in your case is to add the OnTriggerEnter function to the script you want to enable (this event occurs even in disabled scripts):
function OnTriggerEnter(other: Collider){
if (other.name == "Area1"){
this.enabled = true; // enable the script
}
}
NOTE: For this to work, the player must be a CharacterController or Rigidbody, or the trigger must have a kinematic Rigidbody.
Alright I'll try that out tomorrow and I'll get back and see if it worked, but I need to go to sleep before I die x.x, cheers!
Ehhehehe... So the script I want to enable is C#, you wouldn't know what to write for a C# script would you?
Yes yes I realize I should've put that in the OP now >_>
void OnTriggerEnter(Collider other)
{
if(other.name == "Area1")
{
this.enabled = true;
}
}
Ahahamuhahaha! Brilliant simple success. Can I assume that this.enabled = false; will turn the script off if I set void OnTriggerExit(Collider other) at the top?