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 /
  • Help Room /
avatar image
0
Question by Flavioz · Nov 08, 2012 at 09:52 PM · collisionshealth

Help me with health script

Hi guys, I'm new at scripting. I'm trying to make a script that will reduce the life of the first person controller. This is the script of my health manager:

 static var health : int = 0;
 var myCol : Transform;
 var collided : int = 0;
 var target : Transform;
 var myTransform : Transform;
 
 var zero : int = 0;
 var one : int = 1;
 var two : int = 2;
 var three : int = 3;
 var four : int = 4;
 var five : int = 5;
 var six : int = 6;
 var seven : int = 7;
 var eight : int = 8;
 var nine : int = 9;
 var ten : int = 10;
 
 var healthguitext : GUIText;
 var zombie : Transform ;
 
 function Start(){
 health = 10;
 }
 
 function OnControllerColliderHit(hit : ControllerColliderHit) { 
 if(hit.collider.gameObject.tag == "zombie") { 
 health--;
 }
 }
 
 function Update(){
 if (health == 10){healthguitext.text = ten.ToString();}
     else if (health == 9){healthguitext.text = nine.ToString();}
     else if (health == 8){healthguitext.text = eight.ToString();}
     else if (health == 7){healthguitext.text = seven.ToString();}
     else if (health == 6){healthguitext.text = six.ToString();}
     else if (health == 5){healthguitext.text = five.ToString();}
     else if (health == 4){healthguitext.text = four.ToString();}
     else if (health == 3){healthguitext.text = three.ToString();}
     else if (health == 2){healthguitext.text = two.ToString();}
     else if (health == 1){healthguitext.text = one.ToString();}
     }
     
     
 function lessdamage(){
     if(collided == 0) {
             health--;
             collided++;
             yield WaitForSeconds(1);
             collided--;
             }
 }

I tried inserting the collision in the zombie script, but it counts too much collisions and my life goes to 1 in 1 millisecond

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Seth-Bergman · Nov 08, 2012 at 10:13 PM

you could get rid of all that extra code, in Update it should just say:

 function Update(){
 healthguitext.text = health.ToString();
 }

Well, one way to do this would be to create a coroutine using yield WaitForSeconds() as you have attempted here.. I believe this should work fine, you just have to update the Collision function:

 function OnControllerColliderHit(hit : ControllerColliderHit) { 
 if(hit.collider.gameObject.tag == "zombie") { 
 lessdamage();
 }
 }

So, here is the new version:

 static var health : int = 0;
 var collided : boolean = false;
 var healthguitext : GUIText;
 
 function Start(){
 health = 10;
 }
 
 function OnControllerColliderHit(hit : ControllerColliderHit) { 
 if(hit.collider.gameObject.tag == "zombie") { 
 LessDamage();
 }
 }
 
 function Update(){
 healthguitext.text = health.ToString();
     }
 
 
 function LessDamage(){
     if(!collided) {
             health--;
             collided = true;
             yield WaitForSeconds(1);
             collided = false;
             }
 }

this should work

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 Flavioz · Nov 08, 2012 at 11:05 PM 0
Share

It works perfectly thank you very much

avatar image
0

Answer by cullmejake · May 22, 2016 at 08:22 AM

another way to convert to a string is to type health += "";

Comment
Add comment · 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

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

11 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

Related Questions

CS8025 Parsing error!? 1 Answer

Help with my Arrow Fire Script 1 Answer

Can't add script problem 13 Answers

Lag Problem in 2D Game 1 Answer

Null reference exception object reference not set in an instance of an object 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