-1

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");
  }
 }
Oliver Dungey
  • 29
  • 1
  • 1
  • 7

1 Answers1

3

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;

    // ...
}
Soviut
  • 88,194
  • 49
  • 192
  • 260
  • I just implemented the code again to see the error that occurred, it was 'The best overloaded method match for UnityEngine.Object.Destroy(UnityEngine.Object) has some invalid arguments' – Oliver Dungey Nov 13 '16 at 18:18
  • That's because your weapons are set up strangely with their own classes instead of using GameObjects. Instead of destroying `currentWeapon`, destroy the parent. – Soviut Nov 13 '16 at 18:22
  • I've updated the code to show how to delete the child instance of the weapon that you're parenting into the `tecweaponHolder`. – Soviut Nov 13 '16 at 18:25
  • Your solution worked, thanks. I have now made two weapon holder empty game objects, each in a slightly different position. I have added some lines to the code so when the weapon is switched, so is the holder but it is not doing anything, nor is it throwing any errors. (added code above) – Oliver Dungey Nov 13 '16 at 18:41
  • This isn't a forum. If my solution worked, please mark it correct and ask a new question on SO. – Soviut Nov 13 '16 at 20:24