Events triggered by button pressed.

I need a script that activates when a button is pressed. The context in which I would use this is for a flashlight for a first person discovory/exploration type game where the player has to walk in a dark environment using a flashlight by pressing the left mouse button. I am learning how to script from nothing so any advice on a proper learning path would be much appreciated!

Input has to be checked every frame in Update. There are different functions available. Some just return the state of a button (down or up) some will return true only the frame a button has been pressed down or up. Buttons have to be defined in the input manager.

There are also functions for reading certain keyboard-keys

In general just look into the scripting reference

A script that toggles a light could look like this:

    // UnityScript (Unity's Javascript)
    function Update()
    {
        if (Input.GetMouseButtonDown(0)) // 0 - left button; 1 - right button; 2 - middle button
        {
            light.enabled = !light.enabled;
        }
    }

Make sure you attach this script to a GameObject that has a Light component attached :wink:

The same script in C#:

    // C#
    // FlashLightControl.cs
    using UnityEngine;
    
    public class FlashLightControl : MonoBehaviour
    [
        void Update()
        {
            if (Input.GetMouseButtonDown(0)) 
            {
                light.enabled = !light.enabled;
            }
        }
    }

Thank you, it works perfectly!