My firing script for my gun is working up until totalnumberofbullets (this should be the complete total number of bullets excluding the ones in the clip) becomes lower than 7 (my full amount in my clip). I think I could fix this with a bit of messing around however when (for example) I press reload when the total number of bullets is 2 and I have 3 totalnumberofbullets (total of bullets all together) left it will make my bullets (the amount of bullets that is in the clip of the gun) 7 and the totalnumberofbullets will end up being -4. If this is hard to understand maybe try copying my script into unity and testing it. I’m not sure how I can go about fixing this and it seems to be one of the final issues for my gun script. Here’s the code for it (I am still pretty new to programming so the script is probably pretty bad)
using UnityEngine;
using System.Collections;
public class wfa3 : MonoBehaviour
{
public static float bullets = 7f;
public GameObject deagle;
public bool animationreloadplayed = false;
public bool fired = false;
public float Distance;
public float MaxDistance = 100;
public static float Damage = 25;
public bool timedown;
public float time = .84f;
public bool firedshot = false;
public static bool haswaited = false;
public static bool haswaited2 = false;
public bool Reloadinggun = false;
public static bool m1haspressed = false;
public bool firinganimationisplaying = true;
public static bool firinganimationisplayingplayed = false;
public bool Firing = true;
public float totalnumberofbullets = 28f;
public bool reloadable = true;
public float howmuchtotake = 0;
public bool taken = false;
public int TheSize = 100;
public float GUIPosition1;
public float GUIPosition2;
public float numberofbulletstoload = 0f;
void Start ()
{
}
void Update ()
{
if (totalnumberofbullets <= 0f)
{
reloadable = false;
}
if (Input.GetButtonDown ("Fire1") && Reloadinggun == false && m1haspressed == false && bullets > 0f)
{
firedshot = true;
fired = true;
}
if (bullets <= 0f && bullets < 7 && Firing == false && reloadable == true)
{
animationreloadplayed = true;
Force2.IsTiming2 = true;
totalnumberofbullets = totalnumberofbullets - 7;
}
if (fired == true)
{
RaycastHit hit;
{
if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), out hit) && haswaited == true)
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.DrawLine(transform.position, hit.point, Color.red);
fired = false;
}
}
}
}
if (Input.GetKeyDown (KeyCode.R) && bullets <= 6 && reloadable == true && Firing == false && totalnumberofbullets >= 7)
{
animationreloadplayed = true;
Force2.IsTiming2 = true;
howmuchtotake = 7 - bullets;
totalnumberofbullets = totalnumberofbullets - howmuchtotake;
}
if (animationreloadplayed == true)
{
Debug.Log ("Reloading");
deagle.animation.Play ("Reload");
bullets = 7f;
Force2.IsTiming2 = false;
if (bullets == 7 && animationreloadplayed == true)
{
animationreloadplayed = false;
}
}
if (firedshot == true && animationreloadplayed == false)
{
Debug.Log("Fired Shot!");
Force.IsTiming = true;
if (haswaited == true)
{
bullets = bullets -1f;
deagle.animation.Play ("Fire");
firedshot = false;
audio.Play ();
haswaited = false;
firinganimationisplayingplayed = false;
Debug.Log("I have waited!");
}
}
if (totalnumberofbullets <= 7 && Input.GetKeyDown (KeyCode.R) && bullets <= 6 && reloadable == true && Firing == false)
{
animationreloadplayed = true;
Force2.IsTiming2 = true;
numberofbulletstoload = 7-bullets;
totalnumberofbullets = totalnumberofbullets - numberofbulletstoload;
totalnumberofbullets = bullets;
if (bullets == 7 && animationreloadplayed == true)
{
animationreloadplayed = false;
}
}
if (!animation.IsPlaying("Reload"))
{
Reloadinggun = false;
}
else
{
Reloadinggun = true;
}
if (Force.TotalTime >= 0)
{
Firing = true;
}
if (haswaited == true)
{
Firing = false;
}
}
void OnGUI ()
{
GUI.skin.label.fontSize = TheSize;
GUI.Label (new Rect (GUIPosition1, GUIPosition2, 450, 100), " /"+(totalnumberofbullets));
GUI.Label (new Rect (GUIPosition1, GUIPosition2, 450, 100), ""+(bullets));
}
}