• 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 xNewbic · May 27, 2013 at 02:31 PM · javascriptgameobjectcharacterhealth

Problem with PlayerHealth and renderer

I have built a playerHealth script and I did when the player loses life, meshrenderer to blink every 1 second but when he receives damage do more than once because when he feels damage and boolean when is activated, I have made is activated and make multiple times instead of a. How can I do to flash as I want? See the code:

 var curHealth : int = 70;
 var maxHealth : int = 100;
 var deadMode = false;
  
 var curProtect = false;
 static var damageCost = 10;
  
 function Update () {
     if(curHealth >= maxHealth) {
        curHealth = maxHealth;
     }
  
     if(curHealth <= 0) {
        curHealth = 0;
        deadMode = true;
     }
  
     if(curHealth > 0) {
        deadMode = false;
     } 
  
     protectedMode();
 }
  
 function OnTriggerEnter( other : Collider) {
     if(other.tag == "Enemy" && !curProtect) { 
        curProtect = true;
        curHealth -= damageCost;
     }
 }
  
 function protectedMode() {
  
     if(curProtect) {
          graphicsSkin = transform.Find("Graphics");
  
  
          graphicsSkin.renderer.enabled = false;
          yield WaitForSeconds(1);
  
          graphicsSkin.renderer.enabled = true;
          yield WaitForSeconds(1);
          graphicsSkin.renderer.enabled = false;
          yield WaitForSeconds(1);
  
          graphicsSkin.renderer.enabled = true;
          yield WaitForSeconds(1);
          graphicsSkin.renderer.enabled = false;
          yield WaitForSeconds(1);
  
          graphicsSkin.renderer.enabled = true;
          yield WaitForSeconds(1);
  
        curProtect = 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · May 27, 2013 at 02:36 PM

You can use InvokeRepeating(). You can use CancelInvoke() to turn the blinking back off:

 #pragma strict
 
 function Start() { 
     InvokeRepeating("BlinkMe",0.0,1.0);
 } 
 
 function BlinkMe() { 
     renderer.enabled = !renderer.enabled;
 }

Note the last parameter to the InvokeRepeating() is the time between invokes. A smaller number will cause your character to blink faster.

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 xNewbic · May 27, 2013 at 03:07 PM 0
Share

ok. i will test it .. thank you :)

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

13 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Apply Damage To Player On Collison With Specific Game Object 1 Answer

press e to speek -1 Answers

How to copy a Script as a child of another object 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges