pick up a gun problem

Hey everyone, I have a problem with my script and I don’t know how to resolve it. I am new to unity and I may have done a huge mistake that can be very obvious to someone so please help me. Here’s the code:

using UnityEngine;
using System.Collections;

public class GunPuckUp : MonoBehaviour {
	public Transform SpawnTo;
	private RaycastHit hit;
	public Ray ray;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		if(Input.GetKeyUp(KeyCode.E)){
			Vector3 fwd = transform.TransformDirection(Vector3.forward);
			ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			if(Physics.Raycast(ray, hit) && hit.transform.tag == "Gun"){
					hit.transform.parent = SpawnTo;
					hit.transform.position = SpawnTo.transform.position;
					hit.transform.rotation = SpawnTo.transform.rotation;
			}
		}
	}
}

Assuming your gun (SpawnTo) already exists in the scene, then you’ll just need to reposition it to where you need it to show up after it gets parented (also assuming that the parenting happens successfully to begin with).

You can set SpawnTo’s localPosition to move it to the center of the parent like so:

Transform gun = hit.transform;
gun.parent = SpawnTo;
gun.localPosition =  Vector3.zero;

If SpawnTo is actually a prefab that is not in the scene, then you need to make sure its being instantiated first. Something like this might send you on the right direction.

Transform gun = Instantiate(GUNPREFAB, Vector3.zero, Quaternion.identity) as Transform;
gun.parent = SpawnTo;
gun.localPosition =  Vector3.zero

It would help to have a little more information about what exactly isn’t working, if its not actually getting reparented then there may be an issue with the Raycast or your “Gun” tag, though that looks ok at a glance - just verify that the gun GameObject is actually tagged “Gun” in the editor.