Flashlight Malfunction Script

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

Post your code again with code tags, makes it much easier to read :slight_smile:

Ahhh sry bout that Ben. Here it is

var lighttoggle : boolean;
var flashlight : GameObject;
var malfunctioning : boolean;
var overuse : float;

function Update () {
    if (malfunctioning == false)
        {
        if (Input.GetMouseButtonDown(1))
            {
                if (lighttoggle == true)
                {
                    lighttoggle = false;
                    flashlight.light.enabled = false;
                }

                else
                {
                    lighttoggle = true;
                    flashlight.light.enabled = true;
                }
            }
        }
    if (lighttoggle == true)
        {
        InvokeRepeating("overuseadd", 0, Random.Range(0.5,2));
        }
    if (lighttoggle == true)
        {
        if (malfunctioning == false)
            {
            InvokeRepeating("CheckMalfunction", 0, Random.Range(25,60));
            }
        }
}

function overuseadd () {
    if (lighttoggle == true)
    {
    overuse ++;
    }
}
   
function CheckMalfunction () {
    if (overuse >= Random.Range(20,60))
        {
        if (malfunctioning == false)
            {
            malfunctioning = true;
            lighttoggle = false;
            flashlight.light.enabled = false;
            yield WaitForSeconds(Random.Range(45,90));
            malfunctioning = false;
            }
        }
}

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:

//Set the flashlight in the editor
var flashLight : Light;

//Settable in the editor. Between 60 and 90 seconds, the flashlight will completely malfunction.
@Range(10f, 120f)
var useMax : float = 60f;

private var use : float = 0f;

private var on : boolean = false;
private var broken : boolean = false;

private var randomTime : float = 0f;
private var randomTimeSet : boolean = false;

function Update() {

    //Increment Use
    var useChange = (flashLight.enabled) ? Time.deltaTime : -Time.deltaTime * 2f;
    use = Mathf.Clamp( use + useChange, 0f, useMax );

    //Apply Broken
    if (use >= useMax) {
        if (!randomTimeSet) {
            randomTimeSet = true;
            randomTime = Time.time + Random.value * 30f;
      
        } else if (Time.time > randomTime) {
            randomTimeSet = false;
            broken = !broken;
      
        }
    }
          
    //Apply Light  
    if (Input.GetMouseButtonDown(1))
        on = !on;
      
    //The flashlight should be enabled if it is turned on and not malfunctioning.
    flashLight.enabled = on && !broken;
  
}

There is a quick example. If I were you, I’d go back and do some scripting tutorials!

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 >:smile:

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.