- Home /
How to Disable/Enable another Script with Triggers?
Hi.
I'm trying to have a script become enabled only when I enter a trigger, and then become disabled on trigger exit. The purpose of this is to reduce resource consumption so that my script only activates when needed. The purpose of the script being activated is to destroy an object when the user presses the "z" key, giving the illusion of picking it up. However, as stated before, I only want the script "If Z Key is pressed, destroy key" to work if I'm within the trigger. How is this possible?
please add at least some work you've been trying to do
some script
$$anonymous$$y Bad Sorry.
#pragma strict
var IsInside : boolean = false;
var note : GameObject;
var HUD : GameObject;
function Start () {
note.SetActive(false);
}
function OnTriggerEnter ()
{
IsInside = true;
note.SetActive (true);
HUD.GetComponent(Crosshair).enabled = false;
Debug.Log("Enable PickUp Script");
//Where I need to Enable The Item Pick Up Script
}
function OnTriggerExit ()
{
IsInside = false;
note.SetActive(false);
HUD.GetComponent(Crosshair).enabled = true;
Debug.Log("Disable PickU[ Script");
//Where I need to Disable the Item Pick Up Script
}
And The Item Script Being Toggled Enabled/Disabled
#pragma strict
var item : GameObject;
function Start ()
{
}
function Update ()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z))
{
Destroy(item);
}
}
Answer by Zamaroth · Oct 26, 2013 at 11:16 PM
You need to store the triggered object into variable:
function OnTriggerEnter(item : Collider)
Then you simply enable/disable the script. So your OnTrigger functions should look like this:
function OnTriggerEnter (item : Collider)
{
IsInside = true;
note.SetActive (true);
HUD.GetComponent(Crosshair).enabled = false;
Debug.Log("Enable PickUp Script");
item.GetComponent(ItemPickUpScript).enabled = true;
}
function OnTriggerExit (item : Collider)
{
IsInside = false;
note.SetActive(false);
HUD.GetComponent(Crosshair).enabled = true;
Debug.Log("Disable PickU[ Script");
item.GetComponent(ItemPickUpScript).enabled = false;
}
No problem ;)
Don´t forget to mark it as aswered if it helped you.