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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Flashlight origin effect 2 Answers

Does anyone know of some good scripting tutorials? 2 Answers

how to turn off unity answer notification or email?? 1 Answer

Flashlight Flickering 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