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