Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by twoface262 · Jun 23, 2011 at 06:45 PM · toggleflashlight

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eeveelution8 · Feb 10, 2013 at 12:44 AM 0
Share

i used this script, im glad it worked thanks

avatar image
1

Answer by Jessy · Jun 23, 2011 at 06:48 PM

http://unity3d.com/support/documentation/ScriptReference/Component-light.html

http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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'.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
                 }
         }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FlashLight Turning off, but not back on again? (Simple Script Fix?) 4 Answers

Could anyone help me with .js code to be able to pickup a flashlight and turn it on and off -1 Answers

toggle projector as light (C#) 0 Answers

Flash light toggle problem 1 Answer

help with Flashlight Pickup 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges