weapon pickup

how would I make my gun locked and then unlocked when I collide with an object. I have my weapon switch script done but I don’t know how I would make it so the player cant use it until they collide with an object.

here is my weapon switch script it is done in Java script

private var RifleEquip = true; 
 
 
 function Start () 
 {
     SelectWeapon(0);
 }
 
 
 function OnGUI() {
    // if (GUI.RepeatButton (Rect (85,Screen.height - 80,75,75),
     "Shoot")) {
      //   BroadcastMessage("Fire");
    // } 
     if (RifleEquip==true)   {
     if (GUI.Button (Rect (Screen.width - 210,5,50,75), " ")) {
        SelectWeapon(1);
        RifleEquip = false;
     }
     }
     if (RifleEquip==false) {
     if (GUI.Button (Rect (Screen.width - 210,5,50,75), " ")) {
        SelectWeapon(0);
        RifleEquip = true;
     }  
     }
  }
  function SelectWeapon (index : int) {
      for (var i=0;i<transform.childCount;i++)   {
         if (i == index)
           transform.GetChild(i).gameObject.SetActiveRecursively(true);
         else
           transform.GetChild(i).gameObject.SetActiveRecursively(false);
      }
  }

Do this:

  • Attach a rigidbody (set to isKinematic false if you don’t want physics) to your character
  • Have a collider on your character
  • Have a collider set to isTrigger = true on the item you need to touch
  • Set the tag of the touchable item to weapon activate
  • Write an OnTriggerEnter function on your character and check for the touch tag. If you find it set fireable to true.
  • Write an OnTriggerExit function for your character and check for the touch tag and set fireable to false.
  • Use the fireable boolean to turn on your buttons.