Weapon pick up

I’ve made a script which picks a weapon up after you touch it

 var Hand : Transform;
var Sword : Transform;
var customSkin : GUISkin;
function OnTriggerEnter (other : Collider) {
   GetComponent("LockScript").enabled = false;
    if(other.gameObject.tag == "Sword"){
GUI.skin = customSkin;
	GUI.Box (Rect (480,200,400,180), "Do you wish to pick this weapon up?");

		if (GUI.Button (Rect (500,270,100,60), "Yes")) {
		Sword.transform.position = Hand.position;
	    Sword.transform.parent = Hand.transform;
	    Destroy(other.gameObject);}
	
	if (GUI.Button (Rect (750,270,100,60), "No")) {
     GetComponent("LockSript").enabled = true;
    }
}}

It looks as though it should work yet it doesn’t. Any help would be appreciated

You can’t place GUI instructions anywhere you want, only inside the OnGUI function. You must set a boolean variable in OnTriggerEnter to enable the GUI elements inside OnGUI, and clear this variable when it’s no more needed. You must also save a reference to the trigger object, so you can destroy it in OnGUI:

var Hand : Transform;
var Sword : Transform;
var customSkin : GUISkin;
private var pickedObj : GameObject;
private var enableGui = false;

function OnTriggerEnter (other : Collider) {
  GetComponent("LockScript").enabled = false;
  if(other.gameObject.tag == "Sword"){
    enableGui = true;
    pickedObj = other.gameObject;
  }
}

function OnGUI(){
  if (enableGui){
    GUI.skin = customSkin;
    GUI.Box (Rect (480,200,400,180), "Do you wish to pick this weapon up?");
    if (GUI.Button (Rect (500,270,100,60), "Yes")) { // if YES pressed...
      Sword.transform.position = Hand.position;
      Sword.transform.parent = Hand.transform;
      Destroy(pickedObj); // destroy the picked object
      enableGui = false; // close GUI
    }
    if (GUI.Button (Rect (750,270,100,60), "No")) { // if NO pressed...
      GetComponent("LockSript").enabled = true;
      enableGUI = false; // close GUI
    }
  }
}

Where do I put a script like that? On the gun I’m trying to pick up? Or on the player, or something else.

This script should be put on the gun prefab. It gives the player the weapon when you press the button Q.

(Btw, if your using a rigidbody to move the Player you should be fine. If you’re using Raycasting, then you might want to add the a child to the player which has a boxcollider2D over the player and a rigidbody attached with no gravity)

Sorry, for the lengthy explanation, here’s my script -

using UnityEngine;
 
 public class GunPickup : MonoBehaviour
 {
     public SpriteRenderer PistolRenderer;
     public Player Player; //Player is the script which controls the player
 
     private SpriteRenderer _spriteRenderer;
 
     public float FireRate;
     public bool IsSingleBurst; //this is just a variable which tells if the gun is an automatic or a non-automatic
 
     public void Awake()
     {
         _spriteRenderer = GetComponent<SpriteRenderer>();
     }
 
     public void OnTriggerStay2D(Collider2D other)
     {
         if (other.gameObject.GetComponentInParent<Player>() != null && Input.GetKeyDown(KeyCode.Q))
         {
             PistolRenderer.sprite = _spriteRenderer.sprite; // assigns this gun to the player's pistol
             Player.FireRate = FireRate;
             Player.IsSingleBurst = IsSingleBurst;
             if (BurstWhenTaken != null)
                 Instantiate(BurstWhenTaken, transform.position, transform.rotation);
             Destroy(gameObject); // destroys this object
         }
     }
 }