Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 BeanSlice · Apr 25, 2017 at 10:47 AM · c#coroutinecoroutinesflashlightcoroutine errors

Need help using coroutines

Okay, so before I came to this place (again), I gave it a good go attempting to add coroutines to my flashlight script. I started coding in C# yesterday so there is bound to be tons of mistakes but if anybody could tell me how to clearly get this running in the way I want it to, that would be great. Its quite simple - I just want the flashlight to die after it has run out of energy, and the speed at which the flashlight will die to be defined by the user.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; //this allows us to reference anything to do with the UI in our script. *MUST BE ADDED IN ORDER TO USE UI IN SCRIPTS*
 
 public class flashlight2 : MonoBehaviour 
 {
 
     public Light lightSource; //allows us to add a light source to be toggled.
     public AudioClip soundOn; //allows us to add a sound to be played everytime the flashlight is turned on.
     public AudioClip soundOff; //allows us to add a sound to be played everytime the flashlight is turned off.
     public KeyCode key; //this will allow us to choose our desired key to toggle the flashlight outside of the script.
     public Text isontext; //this is used as a reference to the text that will be updated by our script.
     private bool isOn = false; //this will make the text display the light as being off at the beginning of the game, rather than displaying nothing.
     public float currentBatteryLife = 100.0f; //this allows us to set the amount of energy that the flashlight has.
     public float drainSpeed = 3.0f; //this is the speed that the energy drains at. 
     public float maxBatteryLife = 100.0f;
     AudioSource lightaudio;
 
     void Awake () //awake is used to initiate game states and variables before the game starts - this is only ran once during the script's lifetime. We only need to grab the audio source once, so we used awake to do so before anything else can happen.
     {
         lightaudio = GetComponent<AudioSource> (); //grabs the audio source component - allows sound to be played.
     }
 
     void Start () //Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
     {
         lightSource.enabled = false; //this will start the game with the flashlight off.
         displaytext();
         currentBatteryLife = maxBatteryLife;
     }
 
     void Update () //anything within the update function will be called/updated every frame.
     {
         if (Input.GetKeyDown (key)) //if the specified key (set in the Inspector) is pressed, then do the following
         {
             lightSource.enabled = !lightSource.enabled; //the "!" operator means not, you can toggle something by setting it to the opposite value of itself, which is what we are doing here with the light source. You can toggle a light source by toggling its enabled state.
             isOn = !isOn; //the boolean isOn will be toggled when the specified key is pressed.
             displaytext();
             playsound ();
             StartCoroutine (BatteryDrain (drainSpeed));
 
             if (currentBatteryLife <= 0) 
             {        
 
                 lightSource.enabled = false;
             }    
         }
     }
     void displaytext () //using the same line of code more than once is inefficient, therefore I made the line of code a function, so instead of using that same line of code, I can call the function. This uses less memory.
     {
         isontext.text = "Light: " + isOn.ToString (); //the boolean variable isOn will be displayed on the screen whether it is true or false (testing text).
     }
     void playsound () //this function allows us to play a different sound when the flashlight is toggled on or off.
     {
         if (lightSource.enabled == true) //if the flashlight is on, then play the sound indicating that the flashlight has been toggled on.
             lightaudio.PlayOneShot (soundOn);
         if (lightSource.enabled == false) //if the flashlight is off, then play the sound indicating that the flashlight has been toggled off.
             lightaudio.PlayOneShot (soundOff);
     }
     IEnumerator BatteryDrain(float time)
     {
         if (lightSource.enabled) 
         {
             currentBatteryLife -= drainSpeed * Time.deltaTime;
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Apr 25, 2017 at 12:26 PM

Couroutines require yield statements, such as

 yield return new WaitForSeconds (1f);

Time.deltaTime is only accessible within Update(). When you start a coroutine, it will happen independently form the main loop (not another thread though).

Unless the battery decreases step by step over a long period of time, I'd stay away from the coroutines for this.

You could use

 InvokeRepeating ("BatteryDrain", t) // where t is the timestep

I would personally simply put this within Update () and would most probably make currentBatteryLife a property accessor to trigger things when it goes below any given value.

Hope this helps.

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 BeanSlice · Apr 25, 2017 at 02:18 PM 0
Share

Sorry I tried using InvokeRepeating then the function, but I'm not familiar with timesteps. I see that InvokeRepeating requires 3 arguments to run, but I do not know what these timestesps need to be to have the battery drain. Also, I seem to be struggling with getting the BatteryDrain to even drain properly anyway - as I said, I'm very new to this stuff :)

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

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

c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers

Why isn't my coroutine working when I call it from another script. 0 Answers

Unity C# Punching Coroutine not completing 0 Answers

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers

Stackoverflow by too many calculations? 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