How do i add a reload icon for my guns?
So I'm making a game with guns (it's 2D) and i made the basic stuff reload, ammo, but I decided i wanted a reload icon whenever I'm reloading. I tried setting an object active and inactive, but it doesn't seem to work :/
Comment
Here;s my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class Guns : $$anonymous$$onoBehaviour
{
AudioSource audSrc;
public AudioClip shotgun;
public GameObject reloadIcon;
public Transform firePoint;
public Transform firePoint2;
public Transform firePoint3;
public GameObject bulletPrefab;
public int maxAmmo = 7;
private int currentAmmo;
public float reloadTime = 2f;
public float fireRate = 15f;
public float nextFire = 0f;
private bool isRealoading = false;
private bool iconAlive;
void Start()
{
audSrc = gameObject.AddComponent<AudioSource>();
currentAmmo = maxAmmo;
isRealoading = false;
iconAlive = false;
}
void Update()
{
if(isRealoading)
return;
if(currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if(Input.GetButtonDown("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + 1f / fireRate;
Shoot();
StartCoroutine(playAudio(shotgun));
ShakeCamera.instance.StartShake(.5f, .7f);
}
if(iconAlive)
{
reloadIcon.SetActive(true);
}
else if(!iconAlive)
{
reloadIcon.SetActive(false);
}
}
void Shoot()
{
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Instantiate(bulletPrefab, firePoint2.position, firePoint2.rotation);
Instantiate(bulletPrefab, firePoint3.position, firePoint3.rotation);
currentAmmo--;
}
IEnumerator Reload()
{
isRealoading = true;
iconAlive = true;
Debug.Log("Reloading...");
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isRealoading = false;
iconAlive = false;
}
IEnumerator playAudio(AudioClip shotgun)
{
AudioSource.PlayClipAtPoint(shotgun, transform.position);
yield break;
}
}
I basically tried
if(iconAlive)
{
reloadIcon.SetActive(true);
}
else if(!iconAlive)
{
reloadIcon.SetActive(false);
}
`
Answer by TheFatherOfGames · Feb 01, 2020 at 07:09 PM
Nvm i fixed it what i did is i pasted if(iconAlive) { reloadIcon.SetActive(true); } else if(!iconAlive) { reloadIcon.SetActive(false); }
over
if(isRealoading) return;
Turns out it was stopping the whole process :)