Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Timer and Fill Amount.

Discussion in 'Scripting' started by PeppyZee, Apr 6, 2015.

  1. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Code (CSharp):
    1.  
    2.    void UpDate()
    3.     {
    4.         if(IsTimed && IsActive && IsLocked)
    5.         {
    6.             Kp_timer -= Time.deltaTime;
    7.          
    8.             if (Kp_timer < 0) Kp_timer = 0; // clamp the timer to zero
    9.             float seconds = Kp_timer % 60; // calculate the seconds
    10.             float minutes = Kp_timer / 60; // calculate the minutes
    11.             Kp_TimerText.text  = minutes + ":" + seconds;
    12.             //Kp_TimerGauge.fillAmount=Kp_timer;
    13.         }
    14.     }
    15.  
    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?
     
  2. Deleted User

    Deleted User

    Guest

    @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?
     
  3. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    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...

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class UI_Control : MonoBehaviour
    7. {
    8.     public GameObject Kp_SecurityCenter;
    9.     public Canvas Kp_KeyPad;
    10.     public Text Kp_CodeText;
    11.     public Text Kp_TimerText;
    12.     public Image Kp_TimerGauge;
    13.  
    14.     public string Kp_DefualtText = "CODE ?";
    15.     public Color Kp_DefualtColor = new Vector4(0.0F, 255, 0.0F, 1);
    16.     public Color Kp_AlarmColor = new Vector4(255F, 0.0F, 0.0F, 1);
    17.  
    18.     public bool IsActive = true;
    19.     public bool IsSelected = true;
    20.     public bool IsPlayerIn = true;
    21.     public bool IsHackable = true;
    22.     public bool IsLocked = true;
    23.     public bool IsLockable = true;
    24.     public bool IsReLock = false;
    25.     public string uPassCode = "123";
    26.     public string lPassCode= "321";
    27.  
    28.     public bool IsTimed = true;
    29.     public float Kp_timer = 10.0f;
    30.  
    31.     public AudioClip S_Button;
    32.     public AudioClip Su_lock;
    33.     public AudioClip S_Lock;
    34.     public AudioClip Se_Lock;
    35.     public AudioClip Alarm;
    36.  
    37.     public Texture2D MyCursor;
    38.     public Vector2 MyhotSpot = Vector2.zero;
    39.     public CursorMode MycursorMode = CursorMode.Auto;
    40.  
    41.     private string Kp_Code;
    42.     public int Kp_ErrorCount = 0;
    43.     private float a;
    44.    
    45.  
    46.     void Start()
    47.     {
    48.         a = Kp_timer;
    49.  
    50.         Kp_CodeText.color = Kp_DefualtColor;
    51.         Kp_TimerText.color = Kp_DefualtColor;
    52.  
    53.         Kp_CodeText.text = Kp_DefualtText;
    54.         //Cursor.SetCursor(MyCursor, MyhotSpot, MycursorMode);
    55.     }
    56.  
    57.  
    58.     void UpDate()
    59.     {
    60.         Kp_timer -= Time.deltaTime;
    61.  
    62.         if(Kp_timer > 0)
    63.         {
    64.             Kp_TimerText.text= "Time Remaining : "+(int)Kp_timer;
    65.             Kp_TimerGauge.fillAmount = Kp_timer/a;
    66.            
    67.         }
    68.         else{
    69.             Kp_TimerText.text  = "DONE";
    70.         }
    71.     }
    72.  
    73.  
    74.     public void MyKeyCode (string Kp_Code)
    75.     {
    76.         if (Kp_CodeText.text == "CODE ?")
    77.         {
    78.             Kp_CodeText.text = "";
    79.         } else {
    80.         if(Kp_CodeText.text == "ERROR!")
    81.             {
    82.                 Kp_CodeText.text = "";
    83.             }
    84.         }
    85.         Kp_CodeText.text = Kp_CodeText.text += Kp_Code;
    86.         print ("Key");
    87.     }
    88.  
    89.  
    90.     public void Kp_Check()
    91.     {
    92.         if (Kp_CodeText.text == uPassCode)
    93.         {
    94.             Kp_CodeText.text = "UNLOCKED";
    95.             IsLocked=false;
    96.             AudioSource.PlayClipAtPoint (Su_lock, (new Vector3 (0, 0, 0)));
    97.         } else
    98.             {
    99.             if (Kp_CodeText.text == lPassCode)
    100.             {
    101.                 Kp_CodeText.text = "LOCKED";
    102.                 IsLocked=true;
    103.                 AudioSource.PlayClipAtPoint (S_Lock, (new Vector3 (0, 0, 0)));
    104.             }else{
    105.  
    106.                 Kp_CodeText.color=Kp_AlarmColor;
    107.                 Kp_CodeText.text = "ERROR!";
    108.                 AudioSource.PlayClipAtPoint (Alarm, (new Vector3 (0, 0, 0)));
    109.  
    110.                 // Disables panel and not buttons on it.
    111.                 Kp_KeyPad.enabled = false;
    112.  
    113.                 StartCoroutine("ResTime");
    114.             }
    115.         }
    116.     }
    117.  
    118.  
    119.     public void Kp_Clear()
    120.     {
    121.         print ("Cancel");
    122.         Kp_CodeText.text = Kp_DefualtText;
    123.         Kp_CodeText.color = Kp_DefualtColor;
    124.     }
    125.  
    126.  
    127.     public void Kp_Exit()
    128.     {
    129.         print ("Exit");
    130.     }
    131.  
    132.     IEnumerator ResTime()
    133.     {
    134.         yield return new WaitForSeconds(2);
    135.  
    136.         if(IsReLock)
    137.         {
    138.             IsLocked=true;
    139.         }
    140.         Kp_ErrorCount = Kp_ErrorCount + 1;
    141.         Kp_CodeText.color = Kp_DefualtColor;
    142.         Kp_CodeText.text = Kp_DefualtText;
    143.  
    144.         Kp_KeyPad.enabled = true;
    145.     }
    146. }
    147.  
     
    Last edited: Apr 7, 2015
  4. Deleted User

    Deleted User

    Guest

    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.
     
  5. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    LOL ROFLOL that is JUST silly!!!!

    Simple Typo messing me round... Thanks man!
     
    Azurne likes this.
  6. Deleted User

    Deleted User

    Guest

    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 ;) !