Timer and Fill Amount.

   void UpDate()
    {
        if(IsTimed && IsActive && IsLocked)
        {
            Kp_timer -= Time.deltaTime;
          
            if (Kp_timer < 0) Kp_timer = 0; // clamp the timer to zero
            float seconds = Kp_timer % 60; // calculate the seconds
            float minutes = Kp_timer / 60; // calculate the minutes
            Kp_TimerText.text  = minutes + ":" + seconds;
            //Kp_TimerGauge.fillAmount=Kp_timer;
        }
    }

I would like an example of how I can change the fill amount of the UI image via the timer, I cant seem to get it working… help?

@PeppyZee What exactly doens’t work with your timer? Do you get odd outputs or doesn’t the text update at all? And what type is your ‘Kp_timer’ variable?

Kp_TimerText is not showing the Timer Value…
Kp_GuageImage is not charing its fill amount to represent timer.
Kp_KeyPad is being Disabled on code failure where I need just its Keys (buttons) to be disabled.

Seems things I put in UpDate are not working…

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

public class UI_Control : MonoBehaviour
{
    public GameObject Kp_SecurityCenter;
    public Canvas Kp_KeyPad;
    public Text Kp_CodeText;
    public Text Kp_TimerText; 
    public Image Kp_TimerGauge;

    public string Kp_DefualtText = "CODE ?";
    public Color Kp_DefualtColor = new Vector4(0.0F, 255, 0.0F, 1);
    public Color Kp_AlarmColor = new Vector4(255F, 0.0F, 0.0F, 1);

    public bool IsActive = true;
    public bool IsSelected = true;
    public bool IsPlayerIn = true;
    public bool IsHackable = true;
    public bool IsLocked = true;
    public bool IsLockable = true;
    public bool IsReLock = false; 
    public string uPassCode = "123";
    public string lPassCode= "321";

    public bool IsTimed = true;
    public float Kp_timer = 10.0f;

    public AudioClip S_Button;
    public AudioClip Su_lock;
    public AudioClip S_Lock;
    public AudioClip Se_Lock;
    public AudioClip Alarm;

    public Texture2D MyCursor;
    public Vector2 MyhotSpot = Vector2.zero;
    public CursorMode MycursorMode = CursorMode.Auto;

    private string Kp_Code;
    public int Kp_ErrorCount = 0;
    private float a;
   

    void Start()
    {
        a = Kp_timer;

        Kp_CodeText.color = Kp_DefualtColor;
        Kp_TimerText.color = Kp_DefualtColor;

        Kp_CodeText.text = Kp_DefualtText;
        //Cursor.SetCursor(MyCursor, MyhotSpot, MycursorMode);
    }


    void UpDate()
    {
        Kp_timer -= Time.deltaTime;

        if(Kp_timer > 0)
        {
            Kp_TimerText.text= "Time Remaining : "+(int)Kp_timer;
            Kp_TimerGauge.fillAmount = Kp_timer/a;
           
        }
        else{
            Kp_TimerText.text  = "DONE";
        }
    }


    public void MyKeyCode (string Kp_Code)
    {
        if (Kp_CodeText.text == "CODE ?") 
        {
            Kp_CodeText.text = "";
        } else {
        if(Kp_CodeText.text == "ERROR!")
            {
                Kp_CodeText.text = "";
            }
        }
        Kp_CodeText.text = Kp_CodeText.text += Kp_Code;
        print ("Key");
    }


    public void Kp_Check()
    {
        if (Kp_CodeText.text == uPassCode)
        {
            Kp_CodeText.text = "UNLOCKED";
            IsLocked=false;
            AudioSource.PlayClipAtPoint (Su_lock, (new Vector3 (0, 0, 0)));
        } else 
            {
            if (Kp_CodeText.text == lPassCode) 
            {
                Kp_CodeText.text = "LOCKED";
                IsLocked=true;
                AudioSource.PlayClipAtPoint (S_Lock, (new Vector3 (0, 0, 0)));
            }else{

                Kp_CodeText.color=Kp_AlarmColor;
                Kp_CodeText.text = "ERROR!";
                AudioSource.PlayClipAtPoint (Alarm, (new Vector3 (0, 0, 0)));

                // Disables panel and not buttons on it.
                Kp_KeyPad.enabled = false;

                StartCoroutine("ResTime");
            }
        }
    }


    public void Kp_Clear()
    {
        print ("Cancel");
        Kp_CodeText.text = Kp_DefualtText;
        Kp_CodeText.color = Kp_DefualtColor;
    }


    public void Kp_Exit()
    {
        print ("Exit");
    }

    IEnumerator ResTime() 
    {
        yield return new WaitForSeconds(2);

        if(IsReLock)
        {
            IsLocked=true;
        }
        Kp_ErrorCount = Kp_ErrorCount + 1;
        Kp_CodeText.color = Kp_DefualtColor;
        Kp_CodeText.text = Kp_DefualtText;

        Kp_KeyPad.enabled = true;
    }
}

Is “UpDate()” a function which is called by you somewhere? Because if not, you probably want to write “Update()” (Unity’s “builtin” function, which gets called every frame) instead… Note that C# is case sensitive, so mixing up lower- and upper-case will result in errors and/or not working code.

LOL ROFLOL that is JUST silly!!!

Simple Typo messing me round… Thanks man!

1 Like

Haha yeah, those errors are the dumbest, but especially with Unity’s messages quite hard to find sometimes. Glad I could help, even though it wasn’t even a “real” problem…
And of course: Have fun programming your game :wink: !