How to Disable/Enable another Script with Triggers?


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?

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;
    HUD.GetComponent(Crosshair).enabled = true;
    Debug.Log("Disable PickU[ Script");
    item.GetComponent(ItemPickUpScript).enabled = false;