Make sure you do **not** place this script on the light GameObject; any other will do.
using UnityEngine;
using System.Collections;
public class ScriptFlashlight : MonoBehaviour
{
public Light myLight;// the light to be triggered (on and off)
public bool lightEnabled = true;// if the light is enabled or disabled
// Use this for initialization
void Start ()
{
Debug.Log ( "The light started on!" );
}
// Update is called once per frame
void Update ()
{
// check if the 'F' key was pressed
if ( Input.GetKeyDown ( KeyCode.F ) )
{
lightEnabled = !lightEnabled;// toggle the boolean value (this will initially be false)
myLight.enabled = lightEnabled;// enable or disable the light based on the boolean value
// this is just for testing and debug purposes
// check if the light is enabled
if ( lightEnabled )
{
Debug.Log ( "Light turned on..." );
}
else
{
Debug.Log ( "Light turned off..." );
}
}
}
}
↧