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
0
Question by RealMTG · Oct 16, 2013 at 11:20 PM · flashlightbattery

Flashlight Battery Help

Hello!

I am creating a flashlight with batterys and I got some problems. When the flashlight is off nothing happends and when I activate it again, it goes down only like 0.05 on the battery life. Also when I reach 0 it goes to -.

Here is my code: Flashlight (C#)

 using UnityEngine;
 using System.Collections;
 
 public class Flashlight : MonoBehaviour {
     
     private bool FlashlightOn = true;
     public AudioClip clickOn;
     public AudioClip clickOff;
     
     public static float BatteryLife = 1;
     public float BatteryReductionSpeed = 3.0f;
     private float BatteryReductionSpeedOff = 0.0f;
     
     public float onIntensity = 3.0f;
 
     // Use this for initialization
     void Start () {
         light.intensity = onIntensity;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetButtonDown("Toggle Flashlight") && FlashlightOn == false){
             FlashlightOn = true;
             light.intensity = onIntensity;
             audio.PlayOneShot (clickOn);
             BatteryLife = BatteryLife - (BatteryReductionSpeed * Time.deltaTime);
             
             if(BatteryLife <= 0 && FlashlightHud.BatteryCount > 0)
             {
                 FlashlightHud.BatteryCount --;
                 BatteryLife = 100;
             }
         }
         else if(Input.GetButtonDown ("Toggle Flashlight") && FlashlightOn == true) {
             FlashlightOn = false;
             light.intensity = 0;
             audio.PlayOneShot (clickOff);
             BatteryLife = BatteryLife - (BatteryReductionSpeedOff * Time.deltaTime);
         }
     }
 }


FlashLightHud: (C#)

 using UnityEngine;
 using System.Collections;
 
 public class FlashlightHud : MonoBehaviour
 {
     public static int BatteryCount;
     
     void OnGUI()
     {
         GUI.Label(new Rect(10,10,80,30), "Flashlight:" );
         GUI.Label(new Rect(70,10,80,30), Flashlight.BatteryLife.ToString("F2"));
         
         GUI.Label(new Rect(10, 30, 80, 30), "Batteries:");
         GUI.Label(new Rect(70, 30, 80, 30), BatteryCount.ToString());
     }
 }



Thanks!

Comment
Add comment · Show 3
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 harko12 · Oct 17, 2013 at 12:03 AM 1
Share

Looks to me like your update function will only decrement your battery if you have clicked the on or off for the flashlight. If you never click it, then there isn't a case in the update that will decrement the battery over time.

avatar image Benproductions1 · Oct 18, 2013 at 03:00 AM 0
Share

Just as a small coding tip:
Doing BooleanVariable == True is the same as just BooleanVariable.
For BooleanVariable == False it is the same as just !BooleanVariable

Writing any more will just make for more confusing/untidy code

avatar image Tarlius · Oct 18, 2013 at 03:34 AM 0
Share

harko is right I think. Separate the logic for turning the light on and off from the logic for depleting the battery and you'll make the problem much easier to see.

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

How do I make a power out on my flashlight? 1 Answer

Battery life after the game ends PROBLEM 1 Answer

Rechargeble flashlight with battery pick up 1 Answer

All flashlight scripts don't turn off my spotlight 1 Answer

Flashlight Battery Error No definition 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