0
\$\begingroup\$

I just want to change the intensity of a light.

The script is already in the light object, but I can't turn it up or down to save my life, and wherever I look, no one has the answer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flashlight : MonoBehaviour
{

    public bool flashOn;


    void Update()
    {
        if (flashOn)
        {
            light.intensity = 0;


            if (Input.GetKey("q"))
            {
                flashOn = false;
            }
        }
        else
        {
            light.intensity = 1;


            if (Input.GetKey("q"))
            {
                flashOn = true;
            }    
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Did you mean to use GetKeyDown instead of GetKey to avoid double-handling the input it the button is held for a couple of frames? \$\endgroup\$ – DMGregory Dec 7 '19 at 22:15
0
\$\begingroup\$

Is light a reference to a component attached to a game object, presumably the same one this script is attached to? If not, you'll need a reference to it, and you'll need to assign it at some point, like so:

public class Flashlight : MonoBehaviour
{

    public bool flashOn;
    public Light light;

    // After saving this script, the light field should appear 
    // in the inspector alongside your flashOn boolean field.
    // You may assign it manually there, or through code at runtime.
    // I prefer the latter, and it is done like so:
    public void Awake()
    {
        light = GetComponent<Light>();
    }

    void Update()
    {
        if (Input.GetKeyDown("q"))
            flashOn = !flashon;

        if (flashOn)
            light.intensity = 0;
        else
            light.intensity = 1;
    }
}

You may also find it useful to review the Unity API documentation for Light.intensity here.

I adjusted your script a little here, changing GetKey to GetKeyDown. This way it will only change the state of the flashlight once when your key of choice is pressed, rather than toggling it every frame while the key is pressed, which I assume is not the intended functionality as it would be very difficult for a player to press a button for only a single frame.

Good luck with your game!

\$\endgroup\$
0
\$\begingroup\$

For better performance, don't set the light every frame, instead, compare it with another boolean that keeps the last state.

using UnityEngine;

[RequireComponent(typeof(Light))]
public class Flashlight : MonoBehaviour
{

    public bool flashOnOff = true;
    private bool prevFlashOnOff;
    Light lt;

    void Start()
    {
        lt = GetComponent<Light>();
        prevFlashOnOff = !flashOnOff; //will force sync at first frame
    }

    void Update()
    {
        if (Input.GetKey("q")) flashOnOff = !flashOnOff;

        if (flashOnOff != prevFlashOnOff)
        {
            prevFlashOnOff = flashOnOff;
            lt.intensity = flashOnOff ? 1 : 0;
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.