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 /
This question was closed Mar 13, 2012 at 06:03 PM by BigBlob for the following reason:

Other

avatar image
0
Question by BigBlob · Mar 13, 2012 at 12:46 AM · guidecreasehealth

Health bar takes damage

I've got a script that makes the player have a GUI bar on the screen and I need a script that takes damage off the player.

Script :

  var h00 : Texture2D;
 var h10 : Texture2D;
 var h20 : Texture2D;
 var h30 : Texture2D;
 var h40 : Texture2D;
 var h50 : Texture2D;
 var h60 : Texture2D;
 var h70 : Texture2D;
 var h80 : Texture2D;
 var h90 : Texture2D;
 var h100 : Texture2D;
 
 static var HEALTH = 100;
 
 function Update()
 {
     if(HEALTH > 100)
     {
         g_Health.guiTexture.texture = h100;
         return;var g_Health = gameObject.Find("g_Health");
     }
     else if (HEALTH > 90)
     {
         g_Health.guiTexture.texture = h90;
         return;        
     }
     else if (HEALTH > 80)
     {
         g_Health.guiTexture.texture = h80;
         return;        
     }
     else if (HEALTH > 70)
     {
         g_Health.guiTexture.texture = h70;
         return;        
     }
     else if (HEALTH > 60)
     {
         g_Health.guiTexture.texture = h60;
         return;        
     }
     else if (HEALTH > 50)
     {
         g_Health.guiTexture.texture = h50;
         return;        
     }
     else if (HEALTH > 40)
     {
         g_Health.guiTexture.texture = h40;
         return;        
     }
     else if (HEALTH > 30)
     {
         g_Health.guiTexture.texture = h30;
         return;        
     }
     else if (HEALTH > 20)
     {
         g_Health.guiTexture.texture = h20;
         return;        
     }
     else if (HEALTH > 10)
     {
         g_Health.guiTexture.texture = h10;
         return;        
     }
     else if (HEALTH <= 0)
     {
         g_Health.guiTexture.texture = h00;
         Application.LoadLevel(0);
         return;        
     }
 }


And heres the Cube Attack.js Script :

 function OnControllerColliderHit(hit : ControllerColliderHit) {
 
 
 
     if(hit.gameObject.tag == "Player")
 
     hit.transform.gameObject.GetComponent(Player).Substract();
 
 
 
 }
 
 
 
 function Substract() {
 
 
 
     Player.HEALTH -= 10;
 
     if(Player.HEALTH <= 0);
 
     Debug.Log("Dead - Script Works Fine");
 
 }



Can anyone post a script please I'm only 12.

  • Felipe

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

  • Sort: 
avatar image
0

Answer by adrenak · Mar 13, 2012 at 12:50 PM

Do you want something to be able to decrease the health? For that you will have to access the HEALTH variable that you have there and decrease it .

I take here a scenario : there is a cube. every time you enter the cube, your health decreases by 10. I assume that your player is tagged as "Player" and the script above you have is named "healthScript" and it is attached to a FirstPersonCameraController, and that the cube is a trigger collider.

Here's the kind of code you would require for the cube :

 function OnTriggerEnter ( other  : Collider) {
 
     if(other.gameObject.tag == "Player")
         other.transform.root.gameObject.GetComponent(healthScript).decreaseHealth();
         
 }

Then you will have to make an addition to the script that you have above :

 function decreaseHealth () {
 
     HEALTH -= 10;
     if(HEALTH <= 0){
         Debug.Log("DIE");
 }

 

Comment
Add comment · Show 14 · 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 adrenak · Mar 13, 2012 at 12:51 PM 0
Share

BTW your profile says you are 12....

avatar image Kleptomaniac · Mar 13, 2012 at 01:07 PM 0
Share

The plot thickens!

avatar image BigBlob · Mar 13, 2012 at 04:39 PM 0
Share

Sorry there, Also the console shows -$$anonymous$$ Identifier "HEALTH"-. The script works fine except for that.

avatar image fafase · Mar 13, 2012 at 04:49 PM 0
Share

As your variable is static,if HEALTH is in healthScript, where you do your collision use healthScript.HEALTH .

avatar image BigBlob · Mar 13, 2012 at 04:56 PM 0
Share

$$anonymous$$ake sure you test the script with your project and you will get the same problem

Show more comments

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent Help 2 Answers

GUI.Button carrying out two functions? 1 Answer

I am trying to make a stamina bar and it won't drain no matter what i try. 1 Answer

How to slowly decrease health script? 3 Answers

Need help converting unityscript to c# 5 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