Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by supertrentyguy · Oct 25, 2013 at 07:28 PM · flashlightflicker

Flashlight toggle

Now, I did have this script at one time, but I lost it. It was a really good script that allowed you to toggle your flashlight with one click of a button, complete with sounds! But as I said, I lost it. And I have been wandering around this forum thingy looking for the right one, but they don't toggle, they just flicker the flashlight on/off really fast, and they have no sound. And I've tried to learn java before, but my little [AGE WITHHELD] brain couldn't handle it, and trust me, I would love to learn javascript, but it's just a little bit too complicated. Wow, now I have two questions. Actually, I can use google for the second question... But does anyone know a script that does what I described?

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

Answer by Cheetoturkey · Oct 25, 2013 at 07:56 PM

 var flashlightOn : boolean = false;
 
  
 
 function Update () {
 
     //Checks if the boolean is true or false.
 
     if(flashlightOn == true){
 
         light.intensity = 1;//If the boolean is true, then it sets the intensity to what ever you want.
 
     } else {
 
         if(flashlightOn == false){
 
             light.intensity = 0;//If the boolean is false, then it sets the intensity to zero.
 
         }
 
     }
 
     
 
     //Checks if the F key is down and whether the boolean is on or off.
 
     if(Input.GetKeyDown(KeyCode.F) && flashlightOn == false){
 
         flashlightOn = true; //If the f key is down and the boolean is false, it sets the boolean to true.
 
     } else {
 
         if(Input.GetKeyDown(KeyCode.F) && flashlightOn == true) {
 
         flashlightOn = false;//If the f key is down and the boolean is true, it sets the boolean to false.
 
         }
 
     }
 
     
 
 }
 
 }
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 supertrentyguy · Oct 25, 2013 at 10:25 PM 0
Share

Oh my god, thank you! I should really learn unity3d code, so I can stop bothering you guys, but thank you, you saved me a lot of trouble!

avatar image
0

Answer by Unity_scat · Feb 08, 2016 at 05:23 PM

@Cheetoturkey I have a better solution. This code turns on/off the object's Light component. Replace "Flash" with the button that associates with turning on and off. You can edit inputs. Edit --> Project Settings --> Input

 function Update () {
  if (Input.GetButtonDown("Flash")) {
   if (GetComponent.<Light>().enabled == true)
   GetComponent.<Light>().enabled = false;
   
   else
   
   GetComponent.<Light>().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 ItsIcear · Feb 10, 2016 at 05:39 PM

My solution (In C#)

 // Variables
 public bool active = true;
 public GameObject flashlight;

 // Update Function
 void Update()
 {
    // If The Flashlight Button Is Pressed
    if(Input.GetButtonDown("Flashlight"))
    {
         // Toggles The Active Bool
         active = !active;

         // If It Is Active
         if(active)
         {
               flashlight.GetComponent<Light>().enabled = true;
         }

         // If Not
         else
         {
              flashlight.GetComponent<Light>().enabled = false;
         }
     }
 }





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 Hakobik2016 · Jun 17, 2016 at 09:46 PM

Guys. IDK how to find a page so I can ask my own question. SO i went here since it's sorta related to my question

Comment
Add comment · Show 2 · 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 Hakobik2016 · Jun 17, 2016 at 09:49 PM 0
Share

I made a script to make my flashlight flicker when it's low on battery. But ID$$anonymous$$ how to make it toggleable, but also so that it stops running out of battery when it's off. Here's the script without the toggle.

using UnityEngine; using System.Collections;

public class flashlightScript : $$anonymous$$onoBehaviour {

 public float LossInSeconds;
 void Start () {

 }

 //Update Is Classed Once Per Frame

 void Update () {
     GetComponent<Light>() .intensity -= LossInSeconds * Time.deltaTime;

     if(GetComponent<Light>() .intensity <= 1f)
     {
         float FlickeringDuration = Random.Range(0.001f,0.005f);
         StartFlickering (FlickeringDuration);
     }
 }
 void StartFlickering (float Duration)
 {
     GetComponent<Light>() .enabled = false;
     Invoke("EndFlickering",Duration);
 }
 void EndFlickering ()
 {
     GetComponent<Light>() .enabled = true;


 }

}

avatar image Hakobik2016 · Jun 17, 2016 at 09:50 PM 0
Share

Plz Help me.

avatar image
0

Answer by chesterhilly · Jul 24, 2016 at 04:22 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

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

21 People are following this question.

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

Related Questions

(Newbie) Optimize this android LED-toggle? 0 Answers

Decreasing Light Source 0 Answers

I need help with a Flashlight script 1 Answer

Emissive Material Flicker Effect 0 Answers

UNET, flaslight's range and spot 0 Answers


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