- Home /
How to switch between only the weapons that the player currently has
I have a weapon switching script which switches between all the weapons that are children of the weapon holder, either when you scroll the mouse wheel or when press the number keys. However, it always switches between all of the weapons and I only want you to be able to switch between the weapons that you currently have. I have a simple inventory script which contains boolean's which store whether or not you have a weapon, like hasPistol, hasMachineGun, hasShotgun etc. so I only want you be able to use a weapon if the corresponding boolean is true. This is something that I think is used in a lot of games so I'm surprised that there is so little information on how to do something like this.
I've a few different methods and haven't had great results:
I made all of the weapons (the ones attached to the player that you use) children of a separate empty object and when you pick up a weapon it makes the usable weapon the child of the weapon holder that has the switching script attached to it. This worked well except it muddled the order of the weapons, if you say picked up the shotgun before the pistol, the shotgun would become the 1st weapon and the pistol would become the 2nd weapon in the hierarchy and obviously this isn't what I want as the pistol should always be the 1st weapon and the shotgun should always be the 3rd weapon. I looked into using setsiblingindex to rearrange the hierarchy but I couldn't get it to work properly
Having a script on each weapon that has a script with a boolean which stores whether you have the weapon, the weapon switching script accesses this script and doesn't activate the object if the boolean is false, this kind of worked but it still cycled through all of the objects, it didn't activating the object if the boolean wasn't true but it was still selected by the weapon switching script so it appears that you aren't using a weapon.
Here is the weapon switching script:
using UnityEngine;
public class itemSwitching : MonoBehaviour
{
public int selectedWeapon = 0;
public static bool usingKnife;
public static bool usingPistol;
public static bool usingMachineGun;
public static bool usingShotgun;
// Start is called before the first frame update
void Start()
{
selectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
selectedWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >=2)
{
selectedWeapon = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
{
selectedWeapon = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >= 4)
{
selectedWeapon = 3;
}
if (previousSelectedWeapon != selectedWeapon)
{
selectWeapon();
}
}
void selectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}
Your help is very much appreciated :)
Answer by logicandchaos · Feb 19, 2020 at 01:04 PM
You need a list or array that contains all the weapons your character currently has and no more. when your character gets a new weapon you need to add it to the list if they lose a weapon you need to remove it. You could also use booleans like bool doesHaveShotgun; and keep track of it like that instead.
Also you should use on variable instead of 4 booleans, like in your current setup you could set usingMachineGun and usingShotgun to true. You should make an enum and use a switch statement to prevent this.
Thanks for the reply! I've created a list in the weapon switching script and then if the hasPistol bool is set true, the item is added to the list but this is about as far as I got, the item is added to the bottom of the list where as I need it to to be added to a certain place in it (so the switching order is correct). I haven't had much experience with using lists sorry. Is there a way to add a gameobject to a list at a certain position? so that if you picked up the pistol it would add it to the 2nd position on the list or if you picked up the shotgun it added it to the 4th position on the list.
Thanks!