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 xXSilverswordXx · Mar 28, 2017 at 10:51 PM · coroutinedamagecollision2dcooldown

Problem with hit detection and corountines

In my unity 2D project, i have the player avoiding missiles around a map. I want the player to be able to take 3 hits before being taken to the losing screen.

However, i want to add a damage cooldown that prevents the player from taking multiple hits within 2 second intervals. (So after recieving damage, the player becomes immune to damage for the following 2 seconds.)

This script feels correct to me, but there is obviously an error somewhere as it is not working.

Script in question:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class PlayerHealth : MonoBehaviour {
 
     private int Playerhealth = 3;   // This string holds the players current health value. 
     private bool DmgCD = true;    // This string detects if the player can currently recieve damage
     public Sprite[] Healths;
     public GameObject healthbar;
 
     void Start () {
 }
 
     void Update()
     {
         if (Playerhealth == 3)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[3];
         }
         if (Playerhealth == 2)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[2];
         }
         if (Playerhealth == 1)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[1];
         }
         if (Playerhealth == 0)
         {
             healthbar.GetComponent<SpriteRenderer>().sprite = Healths[0];
             //Application.LoadLevel("Lose"); (Remove the comments from this when the lose level is finished) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
         }
     }
 
     private IEnumerator HealthTracker()
     {
         while (true)
         {
             yield return new WaitForSeconds(2f);
             DmgCD = true;
             StopCoroutine(HealthTracker());
         }
     }
 
     void OnCollisionEnter2D(Collision2D col)         // When getting hit by a projectile with the enemy tag
     {
         if (col.gameObject.tag == "Missile")
         {
             //print("damage recieved");
             if (DmgCD == true)                           //If the player is allowed to be damaged
                 DmgCD = false;                            // Make it so the player can no longer recieve new damage
                 Playerhealth = Playerhealth - 1;
                 StartCoroutine(HealthTracker());
             }
         }
     }
 
 
 

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 oussama_bonnor · Mar 29, 2017 at 12:58 AM

Why are you using the while(true) loop ? you can call the WaitForSeconds fonction once and then change the boolean variable's state. without stopping the coroutine and stuff. Can you say what s going on when you run it ?

Comment
Add comment · Show 2 · 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 xXSilverswordXx · Mar 29, 2017 at 06:02 AM 0
Share

I did not think waitforseconds worked without being in a courontine? Could you give me an example of this working please?

As for whats currently going on with the loop, it is working correctly, but ignoring the waitforseconds part.

avatar image oussama_bonnor · Apr 03, 2017 at 11:27 PM 0
Share

Your loop is ignoring the waitForSeconds cause ur stoping the coroutine..delete that line that stops it and it should work fine (sorry for the late answer)

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

100 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

Related Questions

Can you guys help me with my health/damage system? 0 Answers

OnCollisionEnter2D message being ignored 1 Answer

Damage Over Time problem 2 Answers

My enemy don't take damage. 1 Answer

I have a jump scare and I want it to affect my player's health when it activates it and kill my player when health = 0. 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