c# Object moves to global position when unparented

Hello! I am working on a FPS and a weapon dropping system but when i want to unparent a weapon to drop it, instead of moving to the ‘dropPoint’ it moves to the global position of what it was when it was parented. For example: I have a gun at the local position (1,1,1) to my character. When i unparent it, moves to the global position of 1,1,1. I have found that the problem is caused by my ‘Gun script’ as when i disable it it works fine. however, i need the gun script enabled. Does anybody know how to solve this problem? Thank You in advance! :slight_smile:

Here is my gun script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class Weapon : MonoBehaviour {

private Animator anim;
private AudioSource _AudioSource;

public float range = 100f;         //Max range of weapon
public int bulletsPerMag = 30;    //Bullets per magazine
public int bulletsLeft;     //Total bullets left
public int currentBullets;   //Current bullets in mag left

public enum ShootMode { Auto, Semi }
public ShootMode shootingMode;

public Text ammoText;
public Transform shootPoint;     //The point from which the bullets leave the muzzle
public GameObject hitParticles;
public GameObject bulletImpact;

public ParticleSystem muzzleFlash;    //The muzzle flash
public AudioClip shootSound;

public float fireRate = 0.1f;      //The delay between each shot
public float damage = 20f;

float fireTimer;     //Time counter for the delay
private bool isReloading;
private bool isAiming;
private bool shootInput;

private Vector3 originalPosition;
public Vector3 aimPosition;
public float aodSpeed = 8f;

public float spreadFactor = 0.1f;

public bool inInventory;

public UnityStandardAssets.Characters.FirstPerson.FirstPersonController FPC;

private void OnEnable()
{
    UpdateAmmoText();   //Update when weapon switch
}

// Use this for initialization
void Start() {

    anim = GetComponent<Animator>();
    _AudioSource = GetComponent<AudioSource>();

    currentBullets = bulletsPerMag;
    originalPosition = transform.localPosition;

    UpdateAmmoText();    //Show Ammo Text on start
}

// Update is called once per frame
void Update() {

    switch (shootingMode)
    {
        case ShootMode.Auto:
            shootInput = Input.GetButton("Fire1");
        break;

        case ShootMode.Semi:
            shootInput = Input.GetButtonDown("Fire1");
        break;
    }

    if (shootInput)
    {
        if (currentBullets > 0)
            Fire();   //Execute fire function if we press/hold left mouse button
        else if (bulletsLeft > 0)
            DoReload();
    }

    if (Input.GetKeyDown(KeyCode.R))
    {
        if (currentBullets < bulletsPerMag && bulletsLeft > 0)
        DoReload();
    }

    if (fireTimer < fireRate)
        fireTimer += Time.deltaTime;

    AimDownSights();
}

void FixedUpdate()
{
    AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);

    isReloading = info.IsName("Reload");
    anim.SetBool("Aim", isAiming);
    //if (info.IsName("Fire")) anim.SetBool("Fire", false);
}

private void AimDownSights()
{
    if (Input.GetButton("Fire2") && !isReloading)
    {            
        transform.localPosition = Vector3.Lerp(transform.localPosition, aimPosition, Time.deltaTime * aodSpeed);
        isAiming = true;
       
    }
    else
    {
        transform.localPosition = Vector3.Lerp(transform.localPosition, originalPosition, Time.deltaTime * aodSpeed);
        isAiming = false;
    }
}

private void Fire()
{
    //This                   //Or           //This
    if (fireTimer < fireRate || currentBullets <= 0 || isReloading)
        return;

    RaycastHit hit;

    Vector3 shootDirection = shootPoint.transform.forward;
    shootDirection.x += Random.Range(-spreadFactor, spreadFactor);
    shootDirection.y += Random.Range(-spreadFactor, spreadFactor);

      
    

    if (Physics.Raycast(shootPoint.position, shootDirection, out hit, range))
    {
        Debug.Log(hit.transform.name + " found");

        GameObject hitParticleEffect = Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
        //hitParticleEffect.transform.SetParent(hit.transform);
        GameObject bulletHole = Instantiate(bulletImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
        bulletHole.transform.SetParent(hit.transform);

        Destroy(hitParticleEffect, 10f);
        Destroy(bulletHole, 3f);

        if (hit.transform.GetComponent<HealthController>())
        {
            hit.transform.GetComponent<HealthController>().ApplyDamage(damage);
        }
    }

    anim.CrossFadeInFixedTime("Fire", 0.01f);      //Play the fire animation
    muzzleFlash.Play();     //Show muzzle flash
    PlayShootSound();     //Play shooting sound effect

    currentBullets--;           //Minus 1 bullet when shoot

    UpdateAmmoText();           //Update Ammo Text

    fireTimer = 0.0f;        //Reset Fire Timer
}

public void Reload()
{
    if(bulletsLeft <= 0) return;

    int bulletsToLoad = bulletsPerMag - currentBullets;
    int bulletsToDeduct = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft;

    bulletsLeft -= bulletsToDeduct;
    currentBullets += bulletsToDeduct;

    UpdateAmmoText();     //Update the ammo text after reloading
}

private void DoReload()
{
    AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo(0);
     
    if (isReloading) return;

    anim.CrossFadeInFixedTime("Reload", 0.01f);
}

private void PlayShootSound()
{
    _AudioSource.PlayOneShot(shootSound);
    //_AudioSource.clip = shootSound;
    //_AudioSource.Play();
}

private void UpdateAmmoText()
{
    ammoText.text = currentBullets + " /" + bulletsLeft;
   
}

}

After unparenting the object, make him to move to the dropPoint.