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 /
avatar image
1
Question by Reefer · Jun 23, 2014 at 03:07 PM · lightflashlightturnoff

Flashlight won't turn off.

Hi, I'm trying to get my flashlight turn off when I press R key, so far it doesnt do anything if I press it, not even flicker on/off. Heres the FlashLight.js if someone could point me to the right place where the problem is and maybe even tell me how to fix it.

 public var maxBatteryLife : float = 120;
 public var flickerStart : float = 5;
 public var flickerSpeed : float = 0.1;
 public var curBatteryLife : float;
 
 public var batteryTexture : Texture2D;
 public var batteryBarTexture : Texture2D;
 
 function Start()
 {
     curBatteryLife = maxBatteryLife;
 }
 
 function Update()
 {
     if(Input.GetKeyDown("r"))
     {
     if (this.light.enabled == true);
     this.light.enabled = false;
     }
     else
     {
     this.light.enabled = true;    
     }
     
     if(curBatteryLife > 0)
     {
         if(curBatteryLife > maxBatteryLife)
         {
             curBatteryLife = maxBatteryLife;
         }
         else
         {
             curBatteryLife -= Time.deltaTime;
             if(curBatteryLife <= flickerStart)
             {
                 var RandomNumber = Random.value;
                 if(RandomNumber <= flickerSpeed)
                 {
                     this.light.enabled = true;
                 }
                 else
                 {
                     this.light.enabled=false;
                 }
             }
             else
             {
                 this.light.enabled = true;
             }
         }
     }
     else
     {
         curBatteryLife = 0;
         this.light.enabled = false;
     }
 }
 
 function OnGUI()
 {
     GUI.DrawTexture(Rect(Screen.width - 60, 10, 50, 50), batteryTexture);
         
     var adjustBatteryBar = curBatteryLife * (36/maxBatteryLife);
     if(curBatteryLife <= maxBatteryLife/5)
     {
         GUI.color = Color.red;
     }
     else
     {
         GUI.color = Color.white;
     }
     GUI.BeginGroup(Rect(Screen.width - 55, 28, adjustBatteryBar, 12.7));
     GUI.DrawTexture(Rect(0, 0, 36, 12.7), batteryBarTexture);
     GUI.EndGroup();
 }
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

2 Replies

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

Answer by Bunny83 · Jun 23, 2014 at 03:59 PM

You have several problems in your code. First of all Spaghetti code. It's hard to figure out when happens what, Second you use the light's enabled state to tell if the flashlight is (logically) turned on or not. However you also turn it on and off to generate that flicker effect. So you use the same boolean state for two different things. That won't work.

It's best to have a seperate boolean to indicate if the flashlight is on.

 // ...
 var isOn = false;
 
 function Update()
 {
     if(Input.GetKeyDown("r"))
     {
         isOn = !isOn;     // toggle it's state
     }
     light.enabled = isOn;
     if (isOn)
     {
         curBatteryLife -= Time.deltaTime;
         curBatteryLife = Mathf.Clamp(curBatteryLife, 0, maxBatteryLife);
         if (curBatteryLife <= 0)
         {
             isOn = false;
         }
         else if (curBatteryLife < flickerStart && Random.value > flickerSpeed)
         {
             light.enabled = 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 Reefer · Jun 24, 2014 at 02:41 AM 0
Share

Thank you for helping me out. And sorry about spaghetti code, I think I should had been commenting it more.

avatar image
1

Answer by Landern · Jun 23, 2014 at 03:11 PM

change this IF statement:

  if(curBatteryLife > 0)

to this:

  if(curBatteryLife > 0 && this.light.enabled == true)

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 Reefer · Jun 23, 2014 at 03:14 PM 0
Share

That works almost how I want it to work, but now the problem is that it does turn off, but battery goes to 0 and you can't turn it back on ofcourse then. It shouldnt be draining battery at once if you turn it off.

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

23 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 avatar image avatar image

Related Questions

How to turn off flashlight once in water? 1 Answer

How to turn on a light by pressing and after few seconds turn it off 1 Answer

Using shuriken to create volumetric lights effect 5 Answers

Nothing should be seen without the flashlight pointing at it. 1 Answer

-light.intensity, light.spotAngle and light.color with variable? 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