Add value to timer.fillAmount

Hello,

I have an UI image with properties set to:

  • Image type - Filled
  • Fill method - Radial 360
  • Fill origin - Top
  • Fill amount - 1

In my “Timer” C# script I have the following:

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

public class Timer : MonoBehaviour {

    public GameplayManager gameplayManager;
    public float timerDelayBeforeStart;
    public Image timer;
    private bool isTimerDelayedBeforeStartFinished;
    private float privateTimer;

    // If gameobject is enabled
    void OnEnable()
    {
        isTimerDelayedBeforeStartFinished = false;
        Invoke ("SetTimerDelayToFinished",timerDelayBeforeStart);
        timer.fillAmount = 1.0f;
    }
   
    void Update ()
    {
        if (isTimerDelayedBeforeStartFinished == true)
        {
            gameplayManager.timerDuration -= Time.deltaTime;
            privateTimer = gameplayManager.timerDuration;
            timer.fillAmount = privateTimer;
        }
    }

    void SetTimerDelayToFinished()
    {
        isTimerDelayedBeforeStartFinished = true;
    }
}

So basically in the update it will subtract Time.deltaTime from the timeDuration variable in the gameplayManager script and then to set the privateTimer (float) to the gameplayManager.timerDuration

The timer.fillAmount is then equals to the privateTimer (float).

  • My problem is that it does not subtract the privateTimer (float) from the fillAmount.
  • And let say I want to add 5 seconds to the timerDuration it does not update the timer.FillAmount

Any ideas what I am doing wrong?

did you debug privateTimer? what does it say?

When I do:Debug.Log("privateTimer"); it is giving me the timerDuration (with Time.DeltaTime subtracting)

So a few things. why bother with privateTimer variable this:

gameplayManager.timerDuration -= Time.deltaTime;
privateTimer = gameplayManager.timerDuration;
timer.fillAmount = privateTimer;

Is exactly the same as this:

gameplayManager.timerDuration -= Time.deltaTime;
timer.fillAmount = gamePlayManager.timerDuration;

Secondly you say this:

My problem is that it does not subtract the privateTimer (float) from the fillAmount.

but you code never actually does that anywhere. You just set fillAmount = privateTimer?

What exactly are you trying to accomplish. The first part is understandable. When the script is enabled you have a delayTime set, with Invoke call to turn the isTimerDelayedBeforeStartFinished equal to true.

Once that initial delay is done the Update code starts running. But what is THAT suppose to accomplish. What does gamePlayManger.timerDuration start out as? Are you suppose to be subtrcting this time delay from timer.fillAmount. What is timer.fillAmount and what does it do in the timer script?

If you detail what you want to happen in the Update code explicitly (now what is happening), I think we could point you in the right direction on how to code it.

and so is timer.fillAmount the same as Debug.Log(privateTimer);? (<- without “” but i guess you know that)
if they are the same, your code works :slight_smile:
Like takatok said “You just set fillAmount = privateTimer?” (also right about the unnecessary variable privateTimer)

I’m guessing fillAmount can have a value from 0 to 1, so if your time duration is bigger then 1 you won’t see much happen until you wait and drop below 1.

what value’s did privateTimer debug?

for testing i suggest you just do:

timer.fillAmount -= Time.deltaTime;
  • What I am trying to accomplish is that when I add for example 5 gameplay.timerDuration it must also update the fillAmount.
  • The “isTimerDelayedBeforeStartFinished” is for the timer to wait for the loading to completely finished before starting to subtract.
  • The gameplayManager is a script reference (Script is called - GameplayManager). In the GameplayManager script there is a public float timerDuration. In the inspector the timerDuration is set to 15.
  • So when the “isTimerDelayedBeforeStartFinished” is equal to true, then it must start to subtact the Time.deltaTime from the gameplayManager.timerDuration.
  • The fillAmount of the image (timer) should equals the new timerDuration remaining.
  • So lets say my player collides with something it will add 5 to the timerDuration and then it must also shows 5 added to the fillAmount.

it’s point 6 that doesn’t make much sense. the fillAmount property is set between 0 and 1 so adding 5 to it would do much good, it will remain 1 until you go below 1 as in 0.9f…
what you need to do is redesign how you want your player feedback to work.
example:
if there is a maximum to what timerDuration’s value can be you could use that to scale your current timerDuration to between 0 and 1 and than set timer.fillAmount to that.

 void Update ()
    {
        if (isTimerDelayedBeforeStartFinished == true)
        {
            gameplayManager.timerDuration -= Time.deltaTime;
            privateTimer = gameplayManager.timerDuration;
            //add a variable for the max duration to your gameplaymanager
            timer.fillAmount = privateTimer/gameplayManager.timerMax;
        }
    }
1 Like

Yes! Thank you so much. It worked!!!

So new timer code would be:

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

public class Timer : MonoBehaviour {

    // Variables for the script
    public GameplayManager gameplayManager;
    public float timerDelayBeforeStart;
    public Image timer;
    private bool isTimerDelayedBeforeStartFinished;
    private float privateTimer;

    // If gameobject is enabled
    void OnEnable()
    {
        isTimerDelayedBeforeStartFinished = false;
        Invoke ("SetTimerDelayToFinished",timerDelayBeforeStart);
        timer.fillAmount = 1.0f;
    }

    // Update every frame
    void Update ()
    {
        if (isTimerDelayedBeforeStartFinished == true)
        {
            gameplayManager.timerDuration -= Time.deltaTime;
            privateTimer = gameplayManager.timerDuration;
            float timerMax = 15.0f;
            timer.fillAmount = privateTimer/timerMax;
        }
    }

    // This function will get called after timerDelayBeforeStart time
    void SetTimerDelayToFinished()
    {
        isTimerDelayedBeforeStartFinished = true;
    }
}

Working 100%.