Search code examples
c#user-interfaceunity-game-engineprogress-barunity3d-gui

Unity2D C# Reloading progress bar not working propertly


Reloading bar problem

So I'm making a top-down tank shooter game and I want to make a better reloading system than it was before. So I came to the idea that I need some king of progress bar. I knew how to make it so I started doing it. The problem is that it doesn't work properly. As I show in the .gif above, the progress bar don't go down when you shoot second time. Because I'm new to unity, I still don't know everything very good. So I came here, maybe someone could help.

EDIT: I just found another problem and maybe an answer why I have this problem. The second time my script tries to reload, my "needTimer" bool is false, thus the progress bar is not going down when it's false. The new question would be why it becomes false instead of true? My reloading script:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Reload : MonoBehaviour {

    public float ammo;
    public Image progress;
    public bool alreadyReloading;
    public bool needTimer;

    void Start () {
        alreadyReloading = false;
    }
    IEnumerator needtimertime(){
        yield return new WaitForSeconds (6.5f);
        needTimer = false;
    }
    IEnumerator uztaisyt(){
        Debug.Log ("REEELOUUUDING!");
        yield return new WaitForSeconds(6.5f);
        ammo += 1;
        alreadyReloading = false;
    }

    void Update () {
        if (needTimer == true) {
            timer ("");
        }
        if (ammo < 5) {
            if(alreadyReloading == false){
                needTimer = true;
                StartCoroutine(uztaisyt());
                alreadyReloading = true;
            }
        }
        if (progress.fillAmount <= 0) {
            progress.fillAmount = 1.0f; 
        }
    }

    void timer(string tipas){
        progress.fillAmount -=  Time.deltaTime / 6.5f;
        StartCoroutine (needtimertime ());
    }
}

Solution

  • I found my problem and fixed it. The problem was that needTimer was becoming false. So I found where and removed it.

    my new code:

    using UnityEngine;
    

    using UnityEngine.UI; using System.Collections;

    public class Reload : MonoBehaviour {

    public float ammo;
    public Image progress;
    public bool alreadyReloading;
    public bool needTimer;
    
    void Start () {
        alreadyReloading = false;
        needTimer = false;
    }
    
    IEnumerator uztaisyt(){
        Debug.Log ("REEELOUUUDING!");
        yield return new WaitForSeconds(6.5f);
        ammo += 1;
        alreadyReloading = false;
    }
    
    void Update () {
        if (ammo < 5.0f) {
            if(alreadyReloading == false){
                progress.fillAmount = 1.0f;
                needTimer = true;
                StartCoroutine(uztaisyt());
                alreadyReloading = true;
            }
            if (needTimer == true) {
                timer ("");
            }
        }
    }
    
    void timer(string tipas){
        progress.fillAmount -=  Time.deltaTime / 6.5f;
    }
    

    }