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

UI script Not working. Please review

Discussion in 'UGUI & TextMesh Pro' started by PeppyZee, Apr 7, 2015.

  1. 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.

    I cant seem to get these things working, Every thing else works as it should. With the exception of the timer as I am not sure if it is actually functioning at all.

    See Script Notations for more info.. Any suggestions or examples would be appreciated..
    Basically its a script that controls a Code Entry KeyPad.

    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 float a;
    42.  
    43.     private string MyCode;
    44.     public int Kp_ErrorCount = 0;
    45.  
    46.  
    47.     void Start()
    48.     {
    49.         Kp_CodeText.color = Kp_DefualtColor;
    50.         Kp_TimerText.color = Kp_DefualtColor;
    51.         Cursor.SetCursor(MyCursor, MyhotSpot, MycursorMode);
    52.     }
    53.  
    54.  
    55.     void UpDate()
    56.     {
    57.         /*    Was trying this to see if anything would happen, and nothing...
    58.         a = a + 0.01f;
    59.         Kp_TimerGauge.fillAmount = a;
    60.         */
    61.  
    62.         if (IsTimed && IsActive && IsLocked)
    63.         {
    64.             Kp_timer -= Time.deltaTime;
    65.  
    66.             if (Kp_timer < 0)
    67.             {
    68.                 Kp_timer = 0;
    69.             }
    70.  
    71.             float seconds = Kp_timer % 60;
    72.             float minutes = Kp_timer / 60;
    73.  
    74.             // Kp_TimerText is not showing time.
    75.             Kp_TimerText.text = minutes + ":" + seconds;
    76.  
    77.             // Code to change Kp-GuageImage fill amount here...
    78.  
    79.         }
    80.     }
    81.  
    82.  
    83.     public void MyKeyCode (string MyCode)
    84.     {
    85.         if (Kp_CodeText.text == "CODE ?")
    86.         {
    87.             Kp_CodeText.text = "";
    88.         } else {
    89.         if(Kp_CodeText.text == "ERROR!")
    90.             {
    91.                 Kp_CodeText.text = "";
    92.             }
    93.         }
    94.         Kp_CodeText.text = Kp_CodeText.text += MyCode;
    95.         print ("Key");
    96.     }
    97.  
    98.  
    99.     public void Kp_Check()
    100.     {
    101.         if (Kp_CodeText.text == uPassCode)
    102.         {
    103.             Kp_CodeText.text = "UNLOCKED";
    104.             IsLocked=false;
    105.             AudioSource.PlayClipAtPoint (Su_lock, (new Vector3 (0, 0, 0)));
    106.         } else
    107.             {
    108.             if (Kp_CodeText.text == lPassCode)
    109.             {
    110.                 Kp_CodeText.text = "LOCKED";
    111.                 IsLocked=true;
    112.                 AudioSource.PlayClipAtPoint (S_Lock, (new Vector3 (0, 0, 0)));
    113.             }else{
    114.  
    115.                 Kp_CodeText.color=Kp_AlarmColor;
    116.                 Kp_CodeText.text = "ERROR!";
    117.                 AudioSource.PlayClipAtPoint (Alarm, (new Vector3 (0, 0, 0)));
    118.  
    119.                 // Disables panel and not buttons on it.
    120.                 Kp_KeyPad.enabled = false;
    121.  
    122.                 StartCoroutine("ResTime");
    123.             }
    124.         }
    125.     }
    126.  
    127.  
    128.     public void Kp_Clear()
    129.     {
    130.         print ("Cancel");
    131.         Kp_CodeText.text = Kp_DefualtText;
    132.         Kp_CodeText.color = Kp_DefualtColor;
    133.     }
    134.  
    135.  
    136.     public void Kp_Exit()
    137.     {
    138.         print ("Exit");
    139.     }
    140.  
    141.     IEnumerator ResTime()
    142.     {
    143.         yield return new WaitForSeconds(2);
    144.  
    145.         if(IsReLock)
    146.         {
    147.             IsLocked=true;
    148.         }
    149.         Kp_ErrorCount = Kp_ErrorCount + 1;
    150.         Kp_CodeText.color = Kp_DefualtColor;
    151.         Kp_CodeText.text = Kp_DefualtText;
    152.  
    153.         Kp_KeyPad.enabled = true;
    154.     }
    155. }
    156.  
     
  2. PeppyZee

    PeppyZee

    Joined:
    Apr 1, 2015
    Posts:
    20
    Last edited: Apr 8, 2015