Search code examples
c#unity-game-engineswitching

(Unity) weapon switching, destroying previous weapon after switch


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.

Any help is greatly appreciated!

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") >0f)
        {
            EquipWeapon(secondaryWeapon);
            Debug.Log("switched weapon");
        }
        else if (Input.GetAxis("WeaponSwitch")<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<WeaponGraphics>();
        if (currentGraphics == null)
            Debug.LogError("No WeaponGraphics component on the weapon object: " + _weaponIns.name);
 
        if (isLocalPlayer)
            Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
 
    }
 
}
 

UPDATE:

	private Transform currentHolder;

	void Start ()
	{
		currentHolder = awpweaponHolder;
		EquipWeapon(primaryWeapon);
	}
	
	void Update()
	{
		if(Input.GetAxis("WeaponSwitch") >0f)
		{
			currentHolder = tecweaponHolder;
			EquipWeapon(secondaryWeapon);
			Debug.Log("switched weapon");
		}
		else if (Input.GetAxis("WeaponSwitch")<0f)
		{
			currentHolder = awpweaponHolder;
			EquipWeapon(primaryWeapon);
			Debug.Log("switched weapon back");
		}
	}


Solution

  • Nowhere in your code do you actually destroy the old (current) weapon.

    void EquipWeapon(PlayerWeapon _weapon) {
        foreach (Transform child in tecweaponHolder) {
            Destroy(child.gameObject);
        }
    
        currentWeapon = _weapon;
    
        // ...
    }