- Home /
How to make a Flashlight
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)
Answer by lunyrobot · Jul 16, 2012 at 03:27 PM
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
Answer by chesterhilly · Jul 24, 2016 at 04:23 PM
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'. 
Answer by Ratboy601 · Aug 06, 2014 at 05:06 AM
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;
                 }
         }
 }
Answer by JTAGames · Jul 11, 2021 at 04:19 PM
https://www.youtube.com/watch?v=SumkdpuPjLg
4 minute tutorial that will show you how to make a good flashlight.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                