Toggle a script/key on when collide with object.

I’m wanting to disable a script at the start, and then enable it when I run into the set collider item, my script also destroys the collision item at the same time.

I have everything working, except I can’t get it to actually toggle the target script.

This is what I’m trying to do…

Disable the mapToggle key “m” until the player walks over the collision area of the map item that’s on the ground and hits the key “e” to pick up/destroy the item on the ground. Thus enabling the use of the “m” key to toggle the map item.

 using UnityEngine;
    using System.Collections;
    
    public class pickupkey : MonoBehaviour {
    	public Transform WalkedOverObject;
    
    	void Update () {
    	if(Input.GetKeyDown(KeyCode.E))
       {
        if (pickupItem.WalkedOverObject) Destroy(pickupItem.WalkedOverObject);
    		}
    	}	
    }

Try this as your script:

using UnityEngine;
using System.Collections;

public class pickupItem : MonoBehaviour {
     public Transform WalkedOverObject;
     public mapToggle toggleScript;

     void Start() {
          toggleScript = WalkedOverObject.GetComponent<mapToggle>();
          toggleScript.active = false;
     }

     void OnTriggerEnter(Collider other) {
          Destroy(other.gameObject);
          toggleScript.active = true;
     }
}

Okay, than something like this, I guess

I’m not checking if the picked up object is a particular object here yet though.
And I’m assuming here, the mapToggle script is on the same object as the pickup script.

using UnityEngine;
using System.Collections;
 
public class pickupkey : MonoBehaviour 
{
    public GameObject mapObject;
    private mapToggle mapToggleScript;
    void Start()
    {
        mapToggleScript = mapObject.GetComponent<mapToggle>();
        if(mapToggleScript!=null)
        {
             mapObject.renderer.enabled = false; // or mapToggleScript.Start(); if Start is set public
             mapToggleScript.enabled = false;
        }
    }  
    void Update () 
    {
        if(Input.GetKeyDown(KeyCode.E))
        {
            if (pickupItem.WalkedOverObject!=null)   
            {  
                if(mapToggleScript!=null)
                     mapToggleScript.enabled = true;
                Destroy(pickupItem.WalkedOverObject);
            }
       }
    }  
}

Little correction on pickupItem script

using UnityEngine;
using System.Collections;
 
public class pickupItem : MonoBehaviour {
     public static GameObject WalkedOverObject;
 
     void Start() {
     }
 
     void OnTriggerEnter(Collider other) 
     {
          WalkedOverObject = other.gameObject;
     }
     void OnTriggerExit(Collider other) 
     {
          if (WalkedOverObject == other.gameObject) WalkedOverObject = null;
     }
}