- Home /
Problem with reducing points
I am working in a C# script that will reduce 1 to my weapon's ammo everytime the player press the left mouse button. I got no errors, so i think everything is fine, but when i test the game it only reduces one time, not everytime. So if i say that my ammo is 5, when i shoot it reduces to 4 and then it doesn't reduce the ammo anymore.
That's the script i am using
public GameObject txt;
private int ammo;
public int bullets;
void Start()
{
ammo = bullets;
UpdateAmmo();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
ammo = bullets - 1;
UpdateAmmo();
}
}
void UpdateAmmo()
{
txt.GetComponent<UnityEngine.UI.Text>().text = ammo.ToString();
}
}
Answer by Dibbie · Nov 21, 2016 at 08:35 AM
Because your not actually reducing your ammo... "bullets" seems to be what is your actual ammo count, and all your saying is, "whatever the number is for ammo... Show that, but like, just show 1 number less", your not affecting the actual value "bullet" is. So what youd want to do is change ammo = bullets - 1;
to something more like bullets -= 1; ammo = bullets;
that way your actually updating bullets.
You will also run into the problem that, once your bullets hit 0... It will start to go into the negative numbers, if you DONT want that, then contain that idea into a if-statement, something like: if(bullets > 0){bullets -= 1; ammo = bullets;
Hope that helps.
$$anonymous$$y problem is that i want to use a single script for all my weapons, so i created bullet because then i can say what is the maximum bullets each weapon can have, i don't know if this is he best way, probaly not xD
If you want to do that, you may want to look into creating an array of structs maybe, if you want to store information about multiple guns in one script - personally think doing that is a bad idea, but it IS do-able. (all untested code)
public AllWeapons[] weapons = 5; //youd change this number to however many weapons you have in your game
public AllWeapons currentWeapon;
[System.Serializable]
public struct AllWeapons {
public int maxAmmo;
public int currentAmmo;
public float fireRate;
public string weaponName;
// and so on...
};
//in your Start
void Start(){
currentWeapon = weapons[0]; //your first weapon
//from the inspector, you can set all the max ammos and stuff for each weapon
}
//in your Update, similar to what you have now
void Update(){
if(Input.GetButtonDown("Fire1")){
currentWeapon.currentAmmo -= 1;
UpdateAmmo();
}
}
Again... Id contest against doing this kind of system, just because I think its not needed, and it would be better to create a script for each weapon independently, then just reference the ammo on that script, rather then messing with Serializing, and arrays of structs and all that, but like I said, its still possible to do it the way you want.
I fixed it. I realised that making a separate script just for reloading was a really bad idea, so i just copied the lines to my shooting script. I have only a problem now.
The if(bullets > 0){bullets -= 1; ammo = bullets;
doesn't work. Unity doesn't recognize and bugs with the >
. Do you know a way around? Don't let the player shoot indefinetely with negative bullets would be a great thing.
if (Input.GetButtonDown("Fire1"))
{
ammo = ""ammo"" - 1;
UpdateAmmo();
}
You know, it doesn't work at all, but never$$anonymous$$d it's fine, i will fix it eventually. Anyway, at least now my ammo is updated accordingly, thanks!