title: (Unity) weapon switching, destroying previous weapon after switch
body: <p>When I switch weapons, the old weapon does not get destroyed so you end up with 2 functioning guns on the screen. I have tried to destroy the gameobject using Destroy(currentWeapon) but with no luck. Also, I use an empty gameobject attached to the player prefab which is where the gun gets instantiated to. The problem is I need 2 different gameobjects so different guns can sit in different positions. I have made the two empty gameobjects but I need help making them switch when the gun is switched.</p>
  
  <p>Any help is greatly appreciated!</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>using UnityEngine;
  using UnityEngine.Networking;
   
  public class WeaponManager : NetworkBehaviour {
   
      [SerializeField]
      private string weaponLayerName = "Weapon";
   
      [SerializeField]
      private Transform tecweaponHolder;
     
      [SerializeField]
      private Transform awpweaponHolder;
   
      [SerializeField]
      private PlayerWeapon primaryWeapon;
     
      [SerializeField]
      private PlayerWeapon secondaryWeapon;
   
      private PlayerWeapon currentWeapon;
      private WeaponGraphics currentGraphics;
   
      void Start ()
      {
          EquipWeapon(primaryWeapon);
      }
     
      void Update()
      {
          if(Input.GetAxis("WeaponSwitch") &gt;0f)
          {
              EquipWeapon(secondaryWeapon);
              Debug.Log("switched weapon");
          }
          else if (Input.GetAxis("WeaponSwitch")&lt;0f)
          {
              EquipWeapon(primaryWeapon);
              Debug.Log("switched weapon back");
          }
      }
   
      public PlayerWeapon GetCurrentWeapon ()
      {
          return currentWeapon;
      }
   
      public WeaponGraphics GetCurrentGraphics()
      {
          return currentGraphics;
      }
   
      void EquipWeapon (PlayerWeapon _weapon)
      {
          currentWeapon = _weapon;
   
          GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, tecweaponHolder.position, tecweaponHolder.rotation);
          _weaponIns.transform.SetParent(tecweaponHolder);
   
          currentGraphics = _weaponIns.GetComponent&lt;WeaponGraphics&gt;();
          if (currentGraphics == null)
              Debug.LogError("No WeaponGraphics component on the weapon object: " + _weaponIns.name);
   
          if (isLocalPlayer)
              Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
   
      }
   
  }
   </code></pre>
  </div>
  </div>
  </p>
  
  <p>UPDATE:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code> private Transform currentHolder;
  
   void Start ()
   {
    currentHolder = awpweaponHolder;
    EquipWeapon(primaryWeapon);
   }
   
   void Update()
   {
    if(Input.GetAxis("WeaponSwitch") &gt;0f)
    {
     currentHolder = tecweaponHolder;
     EquipWeapon(secondaryWeapon);
     Debug.Log("switched weapon");
    }
    else if (Input.GetAxis("WeaponSwitch")&lt;0f)
    {
     currentHolder = awpweaponHolder;
     EquipWeapon(primaryWeapon);
     Debug.Log("switched weapon back");
    }
   }</code></pre>
  </div>
  </div>
  </p>