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 ddooms · Jun 11, 2015 at 05:31 AM · coroutinetimedamageoverdot

Need help developing Damage Over Time mechanic

Hello,

I am posting anew with this question because the last question was closed (thanks unnamed moderator).

My problem is to do with Damage Over Time. I have been working on this mechanic for a couple of days now, and I can't seem to work my way around this simple problem. I simply don't understand the IEnumerator interface! No matter what concoction I think up,I end up crashing Unity. So here I am, asking for help.

My scenario: I am making a swimming simulation. This simulation needs to be able to drown you, should you stay underwater for too long. Basically, I need the drowning effect to be slow, and odd. I figured I could do this with the coroutines available in C#. In fact, I know that I can. However, I am having trouble understanding them. When I invoke the coroutine, Unity freezes up and crashes. I'm pretty sure that this is because the routine is being called too many times, creating a memory hog.

Basically, it needs to start the coroutine after being underwater for 10 seconds. Once the coroutine starts, it will deal 15 damage every 3-5 seconds. If you stay in the water for too long, you will drown (duh). The routine needs to be able to stop once the player exits the water (moves head above 5.2 on the y axis).

At this point in time, the script crashes unity as soon as I start the program. Any idea why? It isn't supposed to start until the player has been in the water for 10 seconds, as I said earlier.

My code:

 using UnityEngine;
 using System.Collections;
 
 public class suffocation : MonoBehaviour {
 
     public GameObject SwimmerScript;
     public float breathInSeconds = 10.00f;
     public float timeSpentUnderwater;
     public bool underwater = false;
     public float startTime;
     public float targetTime;
     public bool canTakeDrowningDamage = true;  // starts true
 
     void Update(){
 
 
         if(SwimmerScript.GetComponent<Swimmer>().pos < 5.2){
             targetTime += Time.deltaTime;
             underwater = true;
             //targetTime++;
         }
 
         /*
         if(targetTime >= 10.00f && targetTime <= 20.00f)
         {
             StartCoroutine("HurtMe",3.1415);
         }
         if(targetTime > 20.00f)
         {
             StopCoroutine("HurtMe");
         }*/
 
         if (underwater) {
             timeSpentUnderwater += Time.deltaTime;
             if (timeSpentUnderwater > breathInSeconds) {
                 StartCoroutine("Drowning"); // called regardless of whether we can take damage this frame, but that's okay.
             }
         }
         else {
             timeSpentUnderwater = 0f;
         }
     }
     
 
     /*
     void CheckForWater()
     {
         if(underwater && targetTime > 10.00f)
         {
             StartCoroutine(HurtMe(3.1415f));
             //3.1415 seconds later
             
         }
     }
     */
 
         
     
     IEnumerator Drowning() {
         if (!canTakeDrowningDamage) yield break;  // exit if not ready for more damage
         canTakeDrowningDamage = false; // if we touch this line, immediately make it impossible to drown again until true
         gameObject.GetComponent<Health>().health -= 25;  // take damage
         yield return new WaitForSeconds( 3.14f );  // wait for seconds
         canTakeDrowningDamage = true;  // reset to allow damage
 
 
     /*
         while(targetTime < (20.00f) && gameObject.GetComponent<Health>().dead == false){
             gameObject.GetComponent<Health>().health -= 25;
             
             yield return null; //wait for a frame
             yield return new WaitForSeconds(delay);    }
         if(gameObject.GetComponent<Health>().dead == true)
             StopCoroutine("HurtMe");*/
 
 
     }
 }
Comment
Add comment · Show 1
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 zach-r-d · Jun 11, 2015 at 09:04 AM 0
Share

Just to confirm: if you comment out the StartCoroutine line, it no longer crashes Unity?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Run coroutine X amount of times in Y seconds? 3 Answers

Use for-i-loop var outside the loop 2 Answers

stop game for 1 second 2 Answers

Applying Damage 5 Answers

Slowly making the player go faster. 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