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.
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);
}
}
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);
}
}
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
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;
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);
}
}
}
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?
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