- Home /
The question is answered, right answer was accepted. A definition was missing in the inspector,
When I switch weapons the UI ammo count does not change to match the current weapon
At the moment, When I switch weapons, the UI ammo count does not change to match the current weapon I am holding, the switched weapon works and fires as expected but the UI still displays the amount for the previous weapon.
This is the from the gunscript, where the amo is displayed
private void Update()
{
MyInput();
//Set ammo display, if it exists :D
// need to Identify the right weapon before displaying the ammo, to stop showing ammo for wrong weapon.
if (ammunitionDisplay != null)
ammunitionDisplay.SetText(bulletsLeft / bulletsPerTap + " / " + magazineSize / bulletsPerTap);
}
I think the code needs to go where the comment is, to select the right ammo count before displaying it Any help would be awesome, and get a mention in the game credits.
Also If anyone has a better way of doing this I would love to hear it.
This is the weapon switch code if anyone needs to see it.
public class WeaponsSwitching : MonoBehaviour
{
public int selectedWeapon = 0;
// Start is called before the first frame update
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int preveousSelectedWeapon = 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 (preveousSelectedWeapon != 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++;
}
}
}
Answer by Statement · Oct 21, 2020 at 06:11 PM
Make sure that each of your guns have the script that updates the UI with ammo. Maybe you only have that script attached to the first gun?
Your link did not work. Do you get any errors to the console? Perhaps some of the child weapons forgot to set ammunitionDisplay
No errors, everything works but the ammo count does not update whrn weapon is switched
Follow this Question
Related Questions
FPS UI ammo display problem 0 Answers
Image dissappearing when assigning fill amount 0 Answers
Display Ammo Count Above Player 1 Answer
Gun - Ammo , reloading and UI problem. 1 Answer
Please Help figure out whats wrong with Ammo Counter. 2 Answers