• 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 Mohoruto · Mar 09, 2020 at 12:10 PM · timelighttime.deltatimeflashlightcooldown

Flashlight Cooldown and Timer

Hi there - Unity noob here. So in the Indie horror game I'm working on, I have a flashlight attached to my player. So far, I have scripted the flashlight to activate and deactivate with 'E', and it has a cooldown of 10 seconds before you can activate it again. However, before scripting the cooldown, my flashlight had a 'limit' where it would last for 30 seconds and then turn off. However, after adding my cooldown code, the limit is not working and the light stays on forever unless I press 'E'. Can somebody point me in the right direction? Thanks.

Here's my code:

using System.Collections; using System; using System.Collections.Generic; using UnityEngine;

public class FlashlightCooldown : MonoBehaviour {

 public float battery = 30;

 // Start is called before the first frame update
 void Start()
 {
     //Light is automatically off on initialise
     GetComponent<Light>().enabled = false;
 }

 // Update is called once per frame
 public float FlashlightCoolDown = 10f;
 private bool FlashlightOnCooldown;

 void Update()
 {
     //If flashlight is not on cooldown, use flashlight when 'E' is pressed
     if (!FlashlightOnCooldown && Input.GetKey(KeyCode.E))
     {
         StartCoroutine(UseFlashlight());
     }
 }
 IEnumerator UseFlashlight()
 {
     FlashlightOnCooldown = true;

     battery = battery - Time.deltaTime;

     //If the battery is more than 0, it can be activated with 'E'
     if (Input.GetKeyDown(KeyCode.E))
     {
         if (GetComponent<Light>().enabled == false)
         {
             if (battery > 0)
             {
                 GetComponent<Light>().enabled = true;
             }
         }

         else

         //Light is not activated
         {
             GetComponent<Light>().enabled = false;
         }
     }

     //If battery is 0, do not activate light when 'E' is pressed
     if (battery < 0)
     {
         battery = 0;
         GetComponent<Light>().enabled = false;
     }

     yield return new WaitForSeconds(FlashlightCoolDown);
     FlashlightOnCooldown = false;
 }

}

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
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Mar 09, 2020 at 03:24 PM

Your use of the coroutine here is a bit off and is what is causing your issue. Right now what is happening when you press E, the coroutine is being launched once, is getting to the yield return statement and hanging for 10 seconds, then exiting. If you wanted to fix this code you would have to add a while loop to the coroutine so that it runs multiple times, put the yield return inside the while loop, and change it to yield return null as you don't want to wait 10 whole seconds to have the logic be called again. HOWEVER I think use of a coroutine here is a bit overkill and just over-complicates things.

 public float maxBattery = 30;
 public float flashlightCoolDown = 10f;
 
 private float cooldownTimer;
 private bool flashlightOnCooldown = false;
 private bool flashlightIsOn = false;
 private Light light;
 private float battery = maxBattery;
 
 void Start () {
     light = GetComponent<Light> ();
     light.enabled = flashlightIsOn;
 }
 
 void Update () {
 
     //Read input
     if (Input.GetKeyDown (KeyCode.E)) {
         ToggleFlashlight ();
     }
 
     //Do flashlight logic
     DoFlashlightLogic ();
 }
 
 private void DoFlashlightLogic () {
     if (flashlightIsOn) {
         light.enabled = true;
         battery -= Time.deltaTime;
     } else {
         light.enabled = false;
         if (cooldownTimer > 0) cooldownTimer -= Time.deltaTime;
         else flashlightOnCooldown = false;
     }
 
     if (battery <= 0) {
         flashlightIsOn = false;
     }
 }
 
 private void ToggleFlashlight () {
     if (flashlightIsOn) {
         flashlightIsOn = false;
         cooldownTimer = flashlightCoolDown;
     } else {
         if (!flashlightOnCooldown && (battery > 0)) {
             flashlightIsOn = true;
         }
     }
 
 }

Comment
Add comment · Show 17 · 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 Mohoruto · Mar 09, 2020 at 06:30 PM 0
Share

Thanks for your answer @unity_ek98vnTRplGj8Q. Your script is much more simpler than $$anonymous$$e and I can confirm that it works for the most part. The flashlight battery is working which means it runs out after 30 seconds. However, after the 10 second cooldown, I cannot activate the flashlight again. Any idea what I have to add here? Thanks!

avatar image unity_ek98vnTRplGj8Q Mohoruto · Mar 09, 2020 at 06:32 PM 0
Share

Just to clarify, do you want the cooldown timer to start when the flashlight runs out of battery or just when the player presses e to turn the flashlight off? I think I may have misunderstood your original question

avatar image Mohoruto unity_ek98vnTRplGj8Q · Mar 09, 2020 at 07:24 PM 0
Share

I want the cooldown to start after the battery runs out. Then, after 10 seconds, you can turn the flashlight back on - lasting another 30 seconds before applying the 10 second cooldown. Thanks for your fast reply! =)

Show more comments
avatar image
0

Answer by eneIr · Mar 09, 2020 at 12:53 PM

well, it's seem like you forgot to set the battery value back to 30 after the cool down time. However, I think you can use something like void TurnOnFlashLight() and void TurnOffFlashLight() to make it easier (if you want).

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

131 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 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 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 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 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 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

Multiply deltaTime to arrive at point2 in 1.5 sec ? 1 Answer

Having trouble increasing the spawning speed of explosions 0 Answers

Time does not start counting down when need to 1 Answer

Flashlight turning off, but not back on again? 4 Answers

Flashlight origin effect 2 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