UI Confusion Leads to Frustration! <== Needs Help :)

Hello peeps,

I am looking for an example of how to change the “FILL AMOUNT” or an UI image via a timer…

I would like the timer to start when a button is pressed, and I cant seem to get the image fill amount to change the way I would like… Help?

The Script…

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

public class UI_Control : MonoBehaviour
{
    public GameObject     Kp_SecurityCenter;
    public Text            Kp_CodeText;
    public Text         Kp_TimerText; //GUI
    public Image         Kp_TimerGauge;//GUI

    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 MyCode;
    private int Kp_ErrorCount = 0;
    private float t;
    private float minSec;


    void Start()
    {
        Kp_CodeText.color = Kp_DefualtColor;
        Cursor.SetCursor(MyCursor, MyhotSpot, MycursorMode);
    }


    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;
        }
    }
           
    public void MyKeyCode (string MyCode)
    {
        if(Kp_CodeText.text=="CODE ?")
        {
            Kp_CodeText.text = "";
        }

        Kp_CodeText.text = Kp_CodeText.text += MyCode;
        print ("Key");
    }


    public void Kp_Check()
    {
        if (Kp_CodeText.text == uPassCode)
        {
            print ("PASS -Unlock");
            IsLocked=false;
            AudioSource.PlayClipAtPoint (Su_lock, (new Vector3 (0, 0, 0)));
        } else
            {
            if (Kp_CodeText.text == lPassCode)
            {
                IsLocked=true;
                print ("Pass -Lock");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)));
                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;
    }

}

This is the Timer, but cant seem to get it working…

    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;
        }
    }

Well, I think this might be a bit of back-and-forth. Let’s start with the basics – pressing one of the keypad buttons. Did the package import correctly for me? None of your buttons seem to be wired up correctly. Looking at any of the buttons, they seem to have 2 event handlers specified:

  • (None) -
  • AudioSource.Play - Kp_Audio

The audio plays fine, but the button doesn’t change any text because it doesn’t send the text anywhere (that None part). You’ll want to replace that None with Kp_Controller, and then select UI_Control.MyKeyCode from the dropdown menu.

That will get your keys so that they will actually trigger your key input function. It’s still going to crash, but let’s start small. Play around with that a bit and then we’ll move on to getting the actual keypress processing working.