Help with multiple weapons switching

I have a game with lots of weapons and can switch between them fine using my weapon manager script. However this isn’t how I want it to function. However using this way I have free access to all the weapons (I only want melee and pistol to be available from the start), and my other problem is that when I pick up a weapon from a pickup (empty game object on a rotating gun model that makes it active) it adds the picked up weapon to my hands (sets it to active) in addition to my current one (only want it to pick up one, and don’t automatically switch to weapons when they are picked up) but I don’t know how to do this. A friend says I should use inheritance to make a list of objects but I don’t know specifics. Here is the weapon manager script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponManager : MonoBehaviour {

public GameObject melee;
public GameObject pistol;
public GameObject railGun;
public GameObject autoProjectile;
public GameObject machineGun;
public GameObject rocketLauncher;
public GameObject lightningGun;
public GameObject grenadeLauncher;
public GameObject shotgun;
public GameObject autoBounce;

//use list and sort through based on input?
//check if the player has picked up the object in inventory
//Activate ispicked up from other scripts
//check if right weapon is enabled, all other disabled in update
//for loop to check this

//Weapon controller (player) should have an array which contains all the weapon he or she owns,
//when the player walks over a trigger the trigger will use getComponent to get the Weapon controller
//from the player and check the public array to see if the weapon is already in there. If the weapon
//has already been obtained then the weapon does not get picked up and will not dissapear.
//Switch array with List or Dictionary depending on the situation.
//You want to loop through the List of weapons the Player owns and check if it’s name or id is the same
//as the weapon the trigger will give to the player.

//completely different.
// It’s a complex thing to explain but in short you want your weapons to be from the same class (inherited)
//so that you can make a list of type weapon_base
//All weapons would then inherit from weapon_base making them accesible from the list of type weapon_base.

void Start () {
melee.SetActive (false);
pistol.SetActive (true);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}

void Update () {
if (Input.GetButtonDown (“Melee”)) {
melee.SetActive (true);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);

}
if (Input.GetButtonDown(“Pistol”)) {
melee.SetActive (false);
pistol.SetActive (true);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“RailGun”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (true);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“AutoProjectile”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (true);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“MachineGun”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (true);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“RocketLauncher”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (true);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“LightningGun”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (true);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“GrenadeLauncher”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (true);
shotgun.SetActive (false);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“Shotgun”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (true);
autoBounce.SetActive (false);
}
if (Input.GetButtonDown (“AutoBounce”)) {
melee.SetActive (false);
pistol.SetActive (false);
railGun.SetActive (false);
autoProjectile.SetActive (false);
machineGun.SetActive (false);
rocketLauncher.SetActive (false);
lightningGun.SetActive (false);
grenadeLauncher.SetActive (false);
shotgun.SetActive (false);
autoBounce.SetActive (true);
}
}
}

Try this. I’ve cleaned up the code. and added the feature that only allows you to select melee and pistol from the start. you will need to call the pickedUpWeapon() function to enable more weapons.

Sorry for any typo’s I free handed the code. If there are any compile errors. it’s most likely a missed Capital some where.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponManager : MonoBehaviour {

//you could remove all these GameObjects with a List,  but for know i'm leaving them in use
public GameObject melee;
public GameObject pistol;
public GameObject railGun;
public GameObject autoProjectile;
public GameObject machineGun;
public GameObject rocketLauncher;
public GameObject lightningGun;
public GameObject grenadeLauncher;
public GameObject shotgun;
public GameObject autoBounce;

//new list with all GameObjects
private List<GameObject> weaponList;
private List<GameObject> enabledWeaponList;

void Start () {
weaponList = new List<GameObject>();
enabledWeaponList = new List<GameObject>();

//add each weapon to the list
weaponList.Add(melee);
weaponList.Add(pistol);
weaponList.Add(railGun);
weaponList.Add(autoProjectile);
weaponList.Add(machineGun);
weaponList.Add(rocketLauncher);
weaponList.Add(lightningGun);
weaponList.Add(grenadeLauncher);
weaponList.Add(shotgun);
weaponList.Add(autoBounce);

//start with enabling Melee and pistol
pickedUpWeapon(melee);
pickedUpWeapon(pistol);

setActiveWeapon(pistol);
}

void Update () {
if (Input.GetButtonDown ("Melee")) {
setActiveWeapon(melee);
}
if (Input.GetButtonDown("Pistol")) {
setActiveWeapon(pistol);
}
if (Input.GetButtonDown ("RailGun")) {
setActiveWeapon(railGun);
}
if (Input.GetButtonDown ("AutoProjectile")) {
setActiveWeapon(autoProjectile);
}
if (Input.GetButtonDown ("MachineGun")) {
setActiveWeapon(machineGun);
}
if (Input.GetButtonDown ("RocketLauncher")) {
setActiveWeapon(rocketLauncher);
}
if (Input.GetButtonDown ("LightningGun")) {
setActiveWeapon(lightningGun);
}
if (Input.GetButtonDown ("GrenadeLauncher")) {
setActiveWeapon(grenadeLauncher);
}
if (Input.GetButtonDown ("Shotgun")) {
setActiveWeapon(shotgun);
}
if (Input.GetButtonDown ("AutoBounce")) {
setActiveWeapon(autoBounce);
}
}



private void setActiveWeapon(GameObject activeWeapon)
{
    for(int i = 0; i < weaponList.Count; i++)
        {
            for(int j = 0; j < enabledWeaponList.Count; j++)
            {
                if(weaponList[i].name == enabledWeaponList[j].name && enabledWeaponList[j].name == activeWeapon.name)
                {
                    weaponList[i].SetActive (true);
                }else
                {
                    weaponList[i].SetActive (false);
                }
            }
        }
}

//when you pickup your empty gameobject,  run this function to enable the use of the weapon
public void pickedUpWeapon(GameObject weaponPickedUp)
{
    enabledWeaponList.Add(weaponPickedUp);
}


}
1 Like

the code didn’t work correctly. So here is a tested working version

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponManager : MonoBehaviour
{

    //you could remove all these GameObjects with a List,  but for know i'm leaving them in use
    public GameObject melee;
    public GameObject pistol;
    public GameObject railGun;
    public GameObject autoProjectile;
    public GameObject machineGun;
    public GameObject rocketLauncher;
    public GameObject lightningGun;
    public GameObject grenadeLauncher;
    public GameObject shotgun;
    public GameObject autoBounce;

    //new list with all GameObjects
    public List<GameObject> weaponList;
    public List<GameObject> enabledWeaponList;

    void Start()
    {
        weaponList = new List<GameObject>();
        enabledWeaponList = new List<GameObject>();

        //add each weapon to the list
        weaponList.Add(melee);
        weaponList.Add(pistol);
        weaponList.Add(railGun);
        weaponList.Add(autoProjectile);
        weaponList.Add(machineGun);
        weaponList.Add(rocketLauncher);
        weaponList.Add(lightningGun);
        weaponList.Add(grenadeLauncher);
        weaponList.Add(shotgun);
        weaponList.Add(autoBounce);

        //start with enabling Melee and pistol
        pickedUpWeapon(melee);
        pickedUpWeapon(pistol);
        pickedUpWeapon(shotgun);

        setActiveWeapon(pistol);
    }

    void Update()
    {
        if (Input.GetButtonDown("Melee"))
        {
            setActiveWeapon(melee);
        }
        if (Input.GetButtonDown("Pistol"))
        {
            setActiveWeapon(pistol);
        }
        if (Input.GetButtonDown("RailGun"))
        {
            setActiveWeapon(railGun);
        }
        if (Input.GetButtonDown("AutoProjectile"))
        {
            setActiveWeapon(autoProjectile);
        }
        if (Input.GetButtonDown("MachineGun"))
        {
            setActiveWeapon(machineGun);
        }
        if (Input.GetButtonDown("RocketLauncher"))
        {
            setActiveWeapon(rocketLauncher);
        }
        if (Input.GetButtonDown("LightningGun"))
        {
            setActiveWeapon(lightningGun);
        }
        if (Input.GetButtonDown("GrenadeLauncher"))
        {
            setActiveWeapon(grenadeLauncher);
        }
        if (Input.GetButtonDown("Shotgun"))
        {
            setActiveWeapon(shotgun);
        }
        if (Input.GetButtonDown("AutoBounce"))
        {
            setActiveWeapon(autoBounce);
        }
    }



    private void setActiveWeapon(GameObject activeWeapon)
    {
        for (int j = 0; j < weaponList.Count; j++)
        {
            if(weaponList[j] == activeWeapon)
            {
                for (int i = 0; i < enabledWeaponList.Count; i++)
                {
                    if(enabledWeaponList[i] == activeWeapon)
                    {
                        weaponList[j].SetActive(true);
                    }
                }
            }
            else
            {
                weaponList[j].SetActive(false);
            }
        }

    }

    //when you pickup your empty gameobject,  run this function to enable the use of the weapon
    public void pickedUpWeapon(GameObject weaponPickedUp)
    {
        enabledWeaponList.Add(weaponPickedUp);
    }


}

You could save alot of hassle by using a list.

E.g.

[System.Serializable]
Public Struct WeaponData
{
Public Gameobject weapon;
Public String inputKey;
}

List<WeaponData> possibleWeapons;

for(int i = 0; i < possibleWeapons.Count; i++)
{
     if(Input.GetKeyDown(possibleWeapons[i].inputKey))
     {
          SetActiveWeapon(possibleWeapons[i].weapon);
     }
}

This is just an example, but doing something like this will make it mor data driven and save you changing the code for a new weapons.

You can add an enabled bool to the data to save having the extra list for enabled weapons aswell.

1 Like

Thanks alot this seems to work really well. Will have to change my othercode to do the picked up function but the only problem is I can use the shotgun. Also when I press a number of a weapon I don’t have it empties my hand and I’d rather have it keep the current weapon. But thanks this functionality is really useful for a noob like me

nevermind i found it shotgun is set to active in beginning.

Might be a problem as the thing I am picking up is from a trigger on a gameobject on a model but not code with the actual weapon that will be picked

remove line 44 - pickedUpWeapon(shotgun);

edit : I see you saw it.

If you’re doing an OnTriggerEnter() then you should be able to use the code above. You can reference the WeaponManager script.

Another problem. I’m not picking up the actual weapon. I have this script on an empty game object that has the collider of the object on the ground, so if I did get it to call the function it wouldn’t recognise it as the weapon would it. plus this code isn’t actually on the gameobject on the ground. Sorry to keep bringing up problems

public GameObject myWeapon;
public GameObject weaponOnGround;

private float timer = 0f;

void Start () {
myWeapon.SetActive(false);
}

void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == “Player”) {
myWeapon.SetActive(true);
weaponOnGround.SetActive(false);
}
}

void Update () {
transform.Rotate (new Vector3 (0, 30, 0) * Time.deltaTime);
if (!weaponOnGround.activeSelf) {
timer += Time.deltaTime;
if (timer >= 10) {
timer = 0;
weaponOnGround.SetActive(true);
}
}

}
}

I’ll look into this tonight and get you some code

Thanks alot for the help

here are the updates you requested.

using UnityEngine;
using System.Collections;

public class WeaponPickUp : MonoBehaviour {

    public WeaponManager weaponMgnr;
    public GameObject myWeapon; // I'm using this as the referance to weapon in hand.
    //public GameObject weaponOnGround; //not needed, this object is it's self
    private bool isviewable = true;
    private MeshRenderer meshRender;
    private Collider myCollider;
    private float timer = 0f;

    private void Awake()
    {
        weaponMgnr = GameObject.Find("mgnr").GetComponent<WeaponManager>();
    }

    void Start()
    {
        meshRender = GetComponent<MeshRenderer>();
        myCollider = GetComponent<Collider>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Debug.Log("The Player hit me : " + name);
            toggleThings(false);
            weaponMgnr.pickedUpWeapon(myWeapon);
        }
    }

    void Update()
    {
        transform.Rotate(new Vector3(0, 30, 0) * Time.deltaTime);
        if (!isviewable)
        {
            timer += Time.deltaTime;
            if (timer >= 10)
            {
                timer = 0;
                toggleThings(true);
            }
        }
    }

    void toggleThings(bool setToThis)
    {
        isviewable = setToThis;
        myCollider.enabled = setToThis;
        meshRender.enabled = setToThis;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponManager : MonoBehaviour
{

    //you could remove all these GameObjects with a List,  but for know i'm leaving them in use
    public GameObject melee;
    public GameObject pistol;
    public GameObject railGun;
    public GameObject autoProjectile;
    public GameObject machineGun;
    public GameObject rocketLauncher;
    public GameObject lightningGun;
    public GameObject grenadeLauncher;
    public GameObject shotgun;
    public GameObject autoBounce;


    //new list with all GameObjects
    public List<GameObject> weaponList;
    public List<GameObject> enabledWeaponList;

    void Start()
    {
        weaponList = new List<GameObject>();
        enabledWeaponList = new List<GameObject>();

        //add each weapon to the list
        weaponList.Add(melee);
        weaponList.Add(pistol);
        weaponList.Add(railGun);
        weaponList.Add(autoProjectile);
        weaponList.Add(machineGun);
        weaponList.Add(rocketLauncher);
        weaponList.Add(lightningGun);
        weaponList.Add(grenadeLauncher);
        weaponList.Add(shotgun);
        weaponList.Add(autoBounce);

        //start with enabling Melee and pistol
        pickedUpWeapon(melee);
        pickedUpWeapon(pistol);
        //pickedUpWeapon(shotgun);

        setActiveWeapon(pistol);
    }

    void Update()
    {
        if (Input.GetButtonDown("Melee"))
        {
            setActiveWeapon(melee);
        }
        if (Input.GetButtonDown("Pistol"))
        {
            setActiveWeapon(pistol);
        }
        if (Input.GetButtonDown("RailGun"))
        {
            setActiveWeapon(railGun);
        }
        if (Input.GetButtonDown("AutoProjectile"))
        {
            setActiveWeapon(autoProjectile);
        }
        if (Input.GetButtonDown("MachineGun"))
        {
            setActiveWeapon(machineGun);
        }
        if (Input.GetButtonDown("RocketLauncher"))
        {
            setActiveWeapon(rocketLauncher);
        }
        if (Input.GetButtonDown("LightningGun"))
        {
            setActiveWeapon(lightningGun);
        }
        if (Input.GetButtonDown("GrenadeLauncher"))
        {
            setActiveWeapon(grenadeLauncher);
        }
        if (Input.GetButtonDown("Shotgun"))
        {
            setActiveWeapon(shotgun);
        }
        if (Input.GetButtonDown("AutoBounce"))
        {
            setActiveWeapon(autoBounce);
        }
    }



    private void setActiveWeapon(GameObject activateWeapon)
    {
        for (int i = 0; i < enabledWeaponList.Count; i++)
        {
            if(enabledWeaponList[i] == activateWeapon)
            {
                for (int j = 0; j < weaponList.Count; j++)
                {
                    if(weaponList[j] == activateWeapon)
                    {
                        weaponList[j].SetActive(true);
                    }
                    else
                    {
                        weaponList[j].SetActive(false);
                    }
                }
            }
        }
    }

    //when you pickup your empty gameobject,  run this function to enable the use of the weapon
    public void pickedUpWeapon(GameObject weaponPickedUp)
    {
       

        if (enabledWeaponList.Contains(weaponPickedUp))
        {
            // already in the list, do nothing
        }
        else //not in the list so add it
        {
            enabledWeaponList.Add(weaponPickedUp);
        }
    }


}

and here is my test scene so you can see how i hooked everything up.

3032488–226859–Brocolli.unitypackage (242 KB)

Got it to work with the railgun thanks so much. Just need to apply it to all my weapons and I’m good to go start making the visuals

good deal man. glad i could help

1 Like

hi

Hi sir
I need a help in 3rd person shooter
i am using 3 categories of weapon
pistol, rifle and launcher
and each of them have some count of weapons
while the game starts pistol only be active
if i pick up a weapon of type 2 it should added to weapon list 2
and again i am going to pick up a weapon of type 2 it need to pick up the new weapon and drop the old one(replace the weapon in weapon list.
how?

is the code from above still relevant?

@Ajesh_Yohan you should start your own post, don’t high jack this thread

I keep getting this "
NullReferenceException: Object reference not set to an instance of an object
weaponpickup.Awake ()" and it isn’t picking up the object