When I switch weapons, the new weapon object gets instantiated sideways and 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.
Any help is greatly appreciated!
using UnityEngine;
using UnityEngine.Networking;
public class WeaponManager : NetworkBehaviour {
[SerializeField]
private string weaponLayerName = "Weapon";
[SerializeField]
private Transform weaponHolder;
[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, weaponHolder.position, weaponHolder.rotation);
_weaponIns.transform.SetParent(weaponHolder);
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));
}
}