Linking a value to a progress bar! HELP NEEDED!

Hey guys!

I’m a graphical artist who JUST started learning how to code.
I’ve succesfully made a progress bar that works when i change values in the inspector. (Progressbar Script)
I’ve also succesfully made a script that increase the resource “Evocado” by 1 every second.

These are two different scripts. Now the issues that I’m having is that I don’t know how to link the inspector value of the progress bar script to the increase in Evocados.

ProgressBar Script:

using UnityEngine;
using UnityEngine.UI;

[ExecuteInEditMode()]
public class ProgressBar : MonoBehaviour

{
public int maximum;
public int current;
public Image mask;

    // Start is called before the first frame update
    void Start()
    {
      
 
    }

    // Update is called once per frame
    void Update()
    {
        GetCurrentFill();
   
    }

void GetCurrentFill()
{
        float fillAmount = (float)current / (float)maximum;
        mask.fillAmount = fillAmount;
}

}

Here is the script for my Resource Generation:

using UnityEngine;
using UnityEngine.UI;

public class CurrencyHandler : MonoBehaviour
{

    public static long EvoResourceAmount = 0;
    public static long EvoResourceMax = 15;
    public Text EvoResourceAmountText;
    public float EvoTimer;
    public float EvoTimerCount = 0;


    // Start is called before the first frame update
    void Start()
    {

        EvoTimerCount = Time.time+EvoTimer;

            }

    // Update is called once per frame
    void Update()
    {
        EvoResourceAmountText.text = EvoResourceAmount.ToString() + "/" + EvoResourceMax.ToString();
        if (EvoTimerCount < Time.time)
        {
            EvoResourceAmount = EvoResourceAmount + 1;
            EvoTimerCount = Time.time + EvoTimer;
        }



    }
}

I’ve also uploaded an image of the UI, in case that helps.

I’d very much appreciate the help if you’re sitting there with all the knowledge :stuck_out_tongue:

With that said, I hope you have an amazing day!

Your EvoRecource variables are already public and static, meaning you can globally access them by writing, for example, CurrencyHandler.EvoResourceAmount. So the easiest way to get what you want would be to:

// Replace this line:
float fillAmount = (float)current / (float)maximum;

// With something like this:
float fillAmount = (float) CurrencyHandler.EvoResourceAmounts / (float) CurrencyHandler.EvoResourceMax;

Should work, didnt test it in editor tho. Keep in mind that this may result in unintended behavior if amount can become larger than max (which you do not yet prevent). So you either need to make sure that amount cannot become larger than max, or clamp fillAmount to the range of 0 to 1, depending on which is more fitting for your situation. Probably the former.
You would also probably want to update the bar in LateUpdate(), to make sure it runs after Update() and reflects the new changes. This should not make a noticable difference, but generally speaking you want to update visuals after calculations, to reflect the most up-to-date values.

As a small note on code quality, you generally want to name variables using “camelCase”, starting with a lower case letter and capitalizing the first letter of each new word in the name. For method, property and class names you use UpperCamelCase, which is the same but starting with an upper case letter. This is the naming convention used by most C# programmers, makes your code consistent with the Unity API, and also makes it easier to differentiate between methods and variables. There are some more naming conventions, but the above would be the most important imho.
Also, the method name “GetCurrentFill()” is a bit misleading. “Get” implies that the method returns a value representing the current fill amount. Your method however, actually sets the current fill amount, or rather updates it. So “UpdateFillAmount()” may be more appropriate.

Edit: One other thing i noticed is that you use a “long” for your recources. Considering how your max value is supposed to be 15, this is a bit wasteful. Numbers can be stored in “containers” of different sizes. A byte is 8 bit, a short is 16 bit, an int is 32 bit and a long is 64 bit. So using a long, when you hardly need a full byte, is rather wasteful. Unless you want to later increase the max value to a couple billions or more, in which case a long is justified^^
That said, most people dont use bytes or shorts, and mostly use ints for everything. Just thought i’d mention it since i found the usage of long a bit curious and figured maybe you dont know the difference.

Hope this helps :slight_smile:

You’re the boss mate! It worked wonders.

I’m aware I didn’t set a max cap in code yet, that’s one of the next steps.
The reason I’m using a long is that the numbers will get massive with time.

Thanks a bunch for the tip to update after numbers are given, and not before! I’ll take a look at that when I’m done with my current goals.
Thanks as well, for giving me a lil lecture on upper and lower cases. I had no idea :stuck_out_tongue:

You’ve helped me a lot! I appreciate it greatly.

How do I mark a thread solved? :smile:

You cant, if the question has been answered just say thanks like you did, and then that’s it :slight_smile:
Others in the future may find a thread and post a question there, so the threads stay open.

Wish you best of success with your project!