Question by 
               Koehler_Games · Jan 12, 2020 at 12:13 AM · 
                uifpscounterammogun script  
              
 
              How do i implement a ammo counter into my gun script
So far everything I have tried has not worked. Is there a way to make a counter to display current ammo for this script? Could be a separate script or in the same script.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class M4A1ShootScript : MonoBehaviour
 {
 private float NextTimeToFire = 0f;
 private int CurrentAmmo;
 private bool IsReloading = false;
 [Space(10)]
 [Header("Floats")]
 public float Damage = 10.0f;
 public float Range = 100.0f;
 public float ImpactForce = 60f;
 public float FireRate = 15f;
 public float ReloadTime = 1.0f;
 [Space(10)]
 [Header("Others")]
 [Space(5)]
 public Text RemainingAmmo;
 public int MaxAmmo = 10;
 public Camera FPSCamera;
 public Animator GunAnimations;
 public ParticleSystem MuzzleFlash;
 public GameObject impactEffect;
 public bool AllowedToShoot = true;
 public bool RecoilAnimation = true;
 // Start is called before the first frame update
 void Start()
 {
     if (CurrentAmmo == -1)
         CurrentAmmo = MaxAmmo;
 }
 private void OnEnable()
 {
     IsReloading = false;
     GunAnimations.SetBool("Reloading", false);
 }
 // Update is called once per frame
 void Update()
 {
     GunAnimations.SetBool("not", false);
     if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
     {
         if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
         {
             GunAnimations.SetBool("Sprinting", true);
             GunAnimations.SetBool("IsShooting", false);
             GunAnimations.SetBool("UpToDown", true);
         }
     }
     if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow))
     {
         GunAnimations.SetBool("Sprinting", false);
     }
     if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
     {
         GunAnimations.SetBool("Sprinting", false);
     }
     if (RecoilAnimation == false) return;
     {
         if (Input.GetButtonDown("Fire1"))
         {
             GunAnimations.SetBool("Recoil", true);
             GunAnimations.SetBool("IsShooting", false);
             GunAnimations.SetBool("UpToDown", true);
             GunAnimations.SetBool("DownToIdle", true);
         }
         if (Input.GetButtonUp("Fire1"))
         {
             GunAnimations.SetBool("Recoil", false);
             GunAnimations.SetBool("IsShooting", false);
         }
     }
     if (Input.GetButtonDown("Fire1"))
     {
         RemainingAmmo.text = "Ammo : " + MaxAmmo;
         GunAnimations.SetBool("Recoil", true);
         GunAnimations.SetBool("IsShooting", false);
         GunAnimations.SetBool("UpToDown", true);
         GunAnimations.SetBool("DownToIdle", true);
     }
     if (Input.GetButtonUp("Fire1"))
     {
         GunAnimations.SetBool("Recoil", false);
         GunAnimations.SetBool("IsShooting", false);
     }
     if (IsReloading)
         return;
     if (CurrentAmmo <= 0)
     {
         StartCoroutine(Reload());
         return;
     }
     if (AllowedToShoot == false) return; // if not allowed to shoot, don't run the code bellow
     {// your code
         if (Input.GetButton("Fire1") || Input.GetButtonDown("Fire1") && Time.time >= NextTimeToFire)
         {
             NextTimeToFire = Time.time + 1f / FireRate;
             Shoot();
         }
     }
 }
 IEnumerator Reload()
 {
     IsReloading = true;
     Debug.Log("Reloading...");
     GunAnimations.SetBool("Reloading", true);
     GunAnimations.SetBool("IsShooting", false);
     yield return new WaitForSeconds(ReloadTime - .25f);
     GunAnimations.SetBool("Reloading", false);
     yield return new WaitForSeconds(.25f);
     CurrentAmmo = MaxAmmo;
     IsReloading = false;
 }
 void Shoot()
 {
     MuzzleFlash.Play();
     CurrentAmmo--;
     RaycastHit hit;
     if (Physics.Raycast(FPSCamera.transform.position, FPSCamera.transform.forward, out hit, Range))
     {
         Debug.Log(hit.transform.name);
         Target enemy = hit.transform.GetComponent<Target>();
         if (enemy != null)
         {
             enemy.TakeDamage(Damage);
         }
         if (hit.rigidbody != null)
         {
             hit.rigidbody.AddForce(-hit.normal * ImpactForce);
         }
         GameObject ImpactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
         Destroy(ImpactGo, 2f);
     }
 }
 }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
fps block fire when click in a ui button 0 Answers
UI Camera Question 2 Answers
How to do Ammo UI in Unity 5.3.4?,How to do Ammo UI in Unity 5 1 Answer
OnMouseDown raycast doesnt match UI crosshair 1 Answer
How can I add ammo to my gun script? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                