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 13yearoldceo · May 25, 2015 at 02:20 PM · playercoroutinedamageyieldflashing

how to make a character flash red when damaged

ok you see i created this coroutine to loop from red to defualt colors for a period of time when the player is hit the problem enter code hereis that i set the coroutine to wait 0.3 seconds then go to the next color then wait 0.3 seconds but the corotouine only follows this once when it is red then the colors start flashing randomly as if it was going with the frame rate( im not sure) but everything else works fine but i want the flashing to look smooth and consistent thank you.

 if (damagebreak) {
             damagebreaktimer -= 1 * Time.deltaTime; 
             colorchange = true;
         } 
 
         if (colorchange && damagebreak) { 
             
             StartCoroutine ("color");
 
         } else { 
             GetComponent<SpriteRenderer> ().color = white;
         }
 
     } 
 
     IEnumerator color(){   
         while(colorchange && damagebreak) {
             GetComponent<SpriteRenderer> ().color = red;     
             yield return new WaitForSeconds(0.3f); 
             GetComponent<SpriteRenderer> ().color = white;     
             yield return new WaitForSeconds(0.3f); 
         }
 
     }
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 dorpeleg · May 25, 2015 at 03:02 PM

You are probably calling StartCoroutine ("color"); more then once, so you have multiple coroutines running.

Do something like:

      bool coroutineCalled = false;
      if (damagebreak) {
          damagebreaktimer -= 1 * Time.deltaTime; 
          colorchange = true;
      } 
      
      if (colorchange && damagebreak) 
      { 
          if(!coroutineCalled)
          {
              StartCoroutine ("color");
          }
      } 
      else 
      { 
          GetComponent<SpriteRenderer> ().color = white;
      }
      
  } 
      
  IEnumerator color()
  {   
      while(colorchange && damagebreak) 
      {
          coroutineCalled = true;
          GetComponent<SpriteRenderer> ().color = red;     
          yield return new WaitForSeconds(0.3f); 
          GetComponent<SpriteRenderer> ().color = white;     
          yield return new WaitForSeconds(0.3f); 
       }
       coroutineCalled = false;
  }
Comment
Add comment · Show 12 · 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 13yearoldceo · May 25, 2015 at 04:37 PM 0
Share

thanks for the help dude but it still not working

avatar image dorpeleg · May 25, 2015 at 05:02 PM 0
Share

can you show a bit more of the code?

avatar image 13yearoldceo · May 26, 2015 at 10:45 PM 0
Share

alright using UnityEngine; using System.Collections;

 public class playerhealth : $$anonymous$$onoBehaviour { 
 
     public bool lvlthreehearts; 
 
     public float threehearts = 15; 
 
     public float health; 
 
     public bool heart3ishalf = false; 
     public bool heart3isempty = false;
     public bool heart2ishalf = false;
     public bool heart2isempty = false;
     public bool heart1ishalf = false; 
     public bool heart1isempty = false; 
 
     public bool damagebreak = false;
     public float damagebreaktimer = 5f; 
     public float damagebreaktimertotal = 5f;  
     public bool colorchange = false; 
     public float colorchangetimer = 1f;
     public Color red; 
     public Color white;
     // Use this for initialization
     void Start () { 
 
         lvlthreehearts = true;
         damagebreaktimer = damagebreaktimertotal;
     
     }
     
     // Update is called once per frame
     void Update () { 
 
         if (lvlthreehearts) {
             health = threehearts; 
             lvlthreehearts = false; 
         } 
 
     
 
 
 
 
 
 
 
 
 
 
 
         if (health == 12.5) {
             heart3ishalf = true;
         }
         if (health == 10) {
             heart3isempty = true;
         }
 
         if (health == 7.5) {
             heart2ishalf = true;
         }
 
         if (health == 5) {
             heart2isempty = true;
         }
 
         if (health == 2.5) {
             heart1ishalf = true;
         }
         if (health == 0) {
             heart1isempty = true;
         }
 
         if (damagebreaktimer <= 0) { 
             damagebreak = false;  
             colorchange = false;  
             damagebreaktimer = damagebreaktimertotal;
         }
 
         if (damagebreak) {
             damagebreaktimer -= 1 * Time.deltaTime; 
             colorchange = true;
         } 
 
         if (colorchange) { 
             
             StartCoroutine ("color");
 
         } else { 
             GetComponent<SpriteRenderer> ().color = white;
         }
 
     } 
 
     IEnumerator color(){   
          {
             GetComponent<SpriteRenderer> ().color = red;     
             yield return new WaitForSeconds(0.3f); 
             GetComponent<SpriteRenderer> ().color = white;     
             yield return new WaitForSeconds(0.3f); 
         }
 
     }
 
 
     
     void OnCollisionEnter2D (Collision2D coll) {
 
         if(coll.gameObject.tag == "charger" && damagebreak == false){ 
             damagebreak = true;
         health -= 2.5f;
         }
     }
 
 
 }
 
avatar image dorpeleg · May 27, 2015 at 07:11 AM 0
Share

This is still not good.

You are calling the coroutine if 'colorchange' is true.

You are setting 'colorchange' to true once you collide with "charger" (collide > damagebreak = true > colorchange = true > coroutine called)

Then you are waiting for some time before turning 'colorchange' off.

During that time, the coroutine is called multiple times.

Also, your code is written without proper na$$anonymous$$g for the fields, it makes it very hard to read.

$$anonymous$$y next comment will contain the solution.

avatar image dorpeleg · May 31, 2015 at 11:30 AM 1
Share

Seems correct, not sure why it's not working (did not try this myself).

Can you explain a bit more about how you want things to work?

I don't understand why you need it in update anyway...

You can just call the coroutine in the OnCollision....

Show more comments

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

21 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

Related Questions

How can I use a co-routine to yield player damage? 1 Answer

yield return WWW stops Coroutine? 0 Answers

Player wont take damage 2 Answers

Damage script is screwed up...? what to do? 1 Answer

Coroutines and waiting for a function to finish execution 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