Hi
I have made a weapon switch function to my fps, i want to display the current ammo amount at my main camera, its working fine until i switch weapon, then its shows the current ammo for the previous gun until i shoot one time.
Here is my scripts
Shooting/ammo script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GunScript : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 300f;
public Text ammoUI;
private bool isReloading = false;
public int maxAmmo = 100;
private int currentAmmo;
public float reloadTime = 1f;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public static int ammoamount;
private float nextTimeToFire = 0f;
public Animator animator;
public Camera fpsCam;
void Start() {
currentAmmo = maxAmmo;
ammoamount = currentAmmo;
}
void OnEnable() {
isReloading = false;
animator.SetBool("Reloading", false);
}
// Update is called once per frame
void Update()
{
if (isReloading) {
return;
}
if (currentAmmo <= 0) {
StartCoroutine(Reload());
return;
}
if (Input.GetKeyDown(KeyCode.R) && currentAmmo < 10) {
StartCoroutine(Reload());
return;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire ) {
nextTimeToFire = Time.time + 1f/fireRate;
Shoot();
}
}
IEnumerator Reload() {
isReloading = true;
Debug.Log("Reloading");
animator.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTime - .25f);
animator.SetBool("Reloading", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
ammoUI.text = currentAmmo.ToString();
isReloading = false;
}
void Shoot() {
muzzleFlash.Play();
currentAmmo--;
ammoamount = currentAmmo;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) {
print(hit.transform.name);
TargetScript target = hit.transform.GetComponent<TargetScript>();
if (target != null) {
target.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);
ammoUI.text = currentAmmo.ToString();
}
}
}
Weapon switch script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WeaponSwitch : MonoBehaviour
{
public int selectedWeapon = 0;
public Text ammoUI;
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
ammoUI.text = GunScript.ammoamount.ToString();
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;
print(GunScript.ammoamount);
}
if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2) {
selectedWeapon = 1;
print(GunScript.ammoamount);
}
if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 2) {
selectedWeapon = 2;
print(GunScript.ammoamount);
}
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++;
}
}
}