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. Dismiss Notice

Flashlight problem

Discussion in 'Scripting' started by Rafkasik, Aug 5, 2014.

  1. Rafkasik

    Rafkasik

    Joined:
    Jun 29, 2014
    Posts:
    5
    Hello

    I have this flashlight script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Flashlight : MonoBehaviour {
    5.  
    6.     private bool on = true;
    7.     private float durability = 100; // The durability of the flashlight in seconds
    8.     private float currentDurability;
    9.  
    10.     private float timer = 0;
    11.     private bool updateTimer = false;
    12.     private bool continueTimerLooping = true;
    13.     private bool disabled = false;
    14.  
    15.     private float delay = 0.5f; //The delay between turning on/off flashlight
    16.     private float delayTime;
    17.  
    18.     public AudioSource flashlightSound;
    19.  
    20.     void Start () {
    21.         StartCoroutine(Timer(1));
    22.         delayTime = delay + Time.time;
    23.     }
    24.  
    25.     void Update () {
    26.  
    27.         if (updateTimer) {
    28.             updateTimer = false;
    29.             StartCoroutine(Timer(1));
    30.             continueTimerLooping = true;
    31.         }
    32.  
    33.         if (disabled) {
    34.             light.enabled = false;
    35.             on = false;
    36.         }
    37.  
    38.         if (Input.GetButtonDown("Flashlight") && !disabled && delayTime < Time.time) { //Check for a "Flashlight" input (go to edit > project settings > input and add a new input called "Flashlight" and set the positive key to "f" or any key you want)
    39.             on = !on;
    40.             light.enabled = on;
    41.             updateTimer = on;
    42.             continueTimerLooping = on;
    43.             flashlightSound.Play();
    44.             delayTime = delay + Time.time;
    45.         }
    46.  
    47.         if (on) {
    48.             currentDurability = durability - timer;
    49.  
    50.             if (currentDurability <= 0) {
    51.                 disabled = true;
    52.                 Debug.Log("Flashlight disabled");
    53.             }
    54.         }
    55.  
    56.         //Debug.Log("Time left: " + currentDurability + ", Timer: " + timer);
    57.     }
    58.  
    59.     IEnumerator Timer(int seconds) {
    60.         yield return new WaitForSeconds(seconds);
    61.         timer++;
    62.         if (continueTimerLooping) {
    63.             StartCoroutine(Timer(seconds));
    64.         }
    65.     }
    66.  
    67.     void OnGUI () {
    68.         var width = 200;
    69.         var height = 30;
    70.         var widthOffset = 0;
    71.         var heightOffset = 0;
    72.  
    73.         GUI.Label(new Rect(Screen.width - width - widthOffset, Screen.height - height - heightOffset, width, height), "Flashlight durability left: " + currentDurability);
    74.     }
    75. }
    76.  
    How do I change the GUI font face?

    Thanks for the replies

    Cheers
     
  2. dvirus1023

    dvirus1023

    Joined:
    Mar 4, 2013
    Posts:
    51
    Rafkasik likes this.
  3. Rafkasik

    Rafkasik

    Joined:
    Jun 29, 2014
    Posts:
    5
    Thanks :)