I have a flashlight type thing already I was just wondering how I could toggle it off and on? the flashlight thing I have now is a spot light attached to the main camera. is there a code that can toggle it off and on without haveing to do 100 lines of coding? (Not much i know :P)
http://unity3d.com/support/documentation/ScriptReference/Component-light.html
http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html
var light1:Light;
var light2:Light;
var lightTimer : float = 0.0;
var lightBattery : float = 8.0;
public var lightOn : boolean = false;
function Update () {
if (Input.GetButtonDown (“Fire1”))
{
audio.Play();
lightOn = !lightOn;
light1.enabled = lightOn;
light2.enabled = lightOn;
}
}
try this but
Note: you must have the audio source added to it for it to work
Here’s something I wrote yesterday, its much simpler, its C#, and doesn’t use the update function. Obviously the less code being called every single frame, the better, right?
using UnityEngine;
using System.Collections;
public class FlashLight : MonoBehaviour
{
public Light light1;
void Start()
{
light1.enabled = false;
}
public void ToggleFlashLight ()
{
if (light1.enabled == true)
{
light1.enabled = false;
}
else
{
light1.enabled = true;
}
}
}
Here is a simple way to turn a light on and off by Clicking
using UnityEngine;
using System.Collections;
public class Flashlight : MonoBehaviour {
public Light light; //assign gameobject with light component attached
void Update () {
if (Input.GetMouseButtonDown (0)) { //Left mouse button
light.enabled = !light.enabled; //changes light on/off
}
}
}
-
Make sure you add the script to GameObject with the light component.
-
Then assign the same GameObject to ‘light’.