Search Unity

Flashlight Malfunction Script

Discussion in 'Scripting' started by SyndicateXx, Apr 27, 2015.

  1. SyndicateXx

    SyndicateXx

    Joined:
    Apr 27, 2015
    Posts:
    5
    Hey, I am making a basic horror game with a flashlight and all, but I also have a flare gun and want them to be used strategically together. I have a huge script that has no flaws but does absolutely nothing even though it looks like its supposed to. I am planning to recreate a script with the help of the Unity Community. Here is my intentions

    #pragma strict

    // Simple Boolean, Once Right Click, change toggle , Toggle=True (Flashlight On)
    var lighttoggle : boolean;
    // Flashlight light object
    var flashlight : GameObject;
    // This is the malfunctioning part. If Malfunctioning=True, Turning Light on is Disabled
    var malfunctioning : boolean;
    // The Counter that is used to check if you are using flashlight too much
    var overuse : int;

    // This is Okay
    function Start () {
    lighttoggle = false;
    flashlight.light.enabled = false;
    }

    function Update () {
    // This states if Malfunctioning is not on, (Light is working) then the light and toggle is working
    if (malfunctioning == false)
    {
    // Right Clicking turns flashlight on and off (WORKING)
    if (Input.GetMouseButtonDown(1))
    {
    if (lighttoggle == true)
    {
    lighttoggle = false;
    flashlight.light.enabled = false;
    }

    else
    {
    lighttoggle = true;
    flashlight.light.enabled = true;
    }
    }
    }
    // Here is the heavy duty. This says once lightoggle is on, you are added to by overuse random of -1 to +2 every second.
    if (lighttoggle == true)
    {
    InvokeRepeating(overuse++, (-1,2), (0.5,2));
    }
    // This says if the malfunctioning is not on and light is on, check if malfunction needs to be turned on every random secs of 25-60.
    if (lighttoggle = true)
    {
    if (malfunctioning == false)
    {
    InvokeRepeating("CheckMalfunction", 0, (25,60));
    }
    }
    }

    // New Function from above, CheckMalfunction
    function CheckMalfunction () {
    // Overuse is added to per second. This guy will check if overuse isn't over a certain value
    if (overuse >= (20,60))
    {
    // If if is, boom. No working. xD (This is old script, the deactivates of light are in new one)
    malfunctioning = true;
    yield WaitForSeconds((45,90))
    malfunctioning=false;
    }
    }
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  3. SyndicateXx

    SyndicateXx

    Joined:
    Apr 27, 2015
    Posts:
    5
    Ahhh sry bout that Ben. Here it is
    Code (JavaScript):
    1. var lighttoggle : boolean;
    2. var flashlight : GameObject;
    3. var malfunctioning : boolean;
    4. var overuse : float;
    5.  
    6. function Update () {
    7.     if (malfunctioning == false)
    8.         {
    9.         if (Input.GetMouseButtonDown(1))
    10.             {
    11.                 if (lighttoggle == true)
    12.                 {
    13.                     lighttoggle = false;
    14.                     flashlight.light.enabled = false;
    15.                 }
    16.  
    17.                 else
    18.                 {
    19.                     lighttoggle = true;
    20.                     flashlight.light.enabled = true;
    21.                 }
    22.             }
    23.         }
    24.     if (lighttoggle == true)
    25.         {
    26.         InvokeRepeating("overuseadd", 0, Random.Range(0.5,2));
    27.         }
    28.     if (lighttoggle == true)
    29.         {
    30.         if (malfunctioning == false)
    31.             {
    32.             InvokeRepeating("CheckMalfunction", 0, Random.Range(25,60));
    33.             }
    34.         }
    35. }
    36.  
    37. function overuseadd () {
    38.     if (lighttoggle == true)
    39.     {
    40.     overuse ++;
    41.     }
    42. }
    43.    
    44. function CheckMalfunction () {
    45.     if (overuse >= Random.Range(20,60))
    46.         {
    47.         if (malfunctioning == false)
    48.             {
    49.             malfunctioning = true;
    50.             lighttoggle = false;
    51.             flashlight.light.enabled = false;
    52.             yield WaitForSeconds(Random.Range(45,90));
    53.             malfunctioning = false;
    54.             }
    55.         }
    56. }
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Okay, I see a few problems and a lot of redundancy here. I seems to me like you could benefit from some scripting tutorials!

    From what I gather, you'd like the right-click to turn off and on the flashlight, and you'd like the flashlight to malfunction at random if it's been used too much. If it malfunctions it's disabled for a random amount of time:


    Code (CSharp):
    1. //Set the flashlight in the editor
    2. var flashLight : Light;
    3.  
    4. //Settable in the editor. Between 60 and 90 seconds, the flashlight will completely malfunction.
    5. @Range(10f, 120f)
    6. var useMax : float = 60f;
    7.  
    8. private var use : float = 0f;
    9.  
    10. private var on : boolean = false;
    11. private var broken : boolean = false;
    12.  
    13. private var randomTime : float = 0f;
    14. private var randomTimeSet : boolean = false;
    15.  
    16. function Update() {
    17.  
    18.     //Increment Use
    19.     var useChange = (flashLight.enabled) ? Time.deltaTime : -Time.deltaTime * 2f;
    20.     use = Mathf.Clamp( use + useChange, 0f, useMax );
    21.  
    22.     //Apply Broken
    23.     if (use >= useMax) {
    24.         if (!randomTimeSet) {
    25.             randomTimeSet = true;
    26.             randomTime = Time.time + Random.value * 30f;
    27.      
    28.         } else if (Time.time > randomTime) {
    29.             randomTimeSet = false;
    30.             broken = !broken;
    31.      
    32.         }
    33.     }
    34.          
    35.     //Apply Light  
    36.     if (Input.GetMouseButtonDown(1))
    37.         on = !on;
    38.      
    39.     //The flashlight should be enabled if it is turned on and not malfunctioning.
    40.     flashLight.enabled = on && !broken;
    41.  
    42. }
    There is a quick example. If I were you, I'd go back and do some scripting tutorials!
     
    Last edited: May 20, 2015
  5. SyndicateXx

    SyndicateXx

    Joined:
    Apr 27, 2015
    Posts:
    5
    Yup, but this malfunctioning malfunctions xD. I can't seem to get what is wrong, and its really bugging me

    This script seems to be super fancy. I'll work it out >:D
     
  6. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    There are quite a few things wrong with your script, I'm afraid. As I said, there is a great deal of redundant/duplicate code, and you're making multiple InvokeRepeating calls on consecutive frames.

    That's why I'm suggesting tutorials.