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 JakeParrott · Nov 14, 2013 at 02:50 PM · javascriptguihealth

Please help me make a health system with GUI. I cant do it!

Hello people, I desperately need your help as I'm going insane!

For the past week I've been sat at my computer trying to create a health system for a 2.5D game I'm making for a university assignment. But i just cant seem to get there.

Now I want my own custom GUI that I've made to display HEALTH as well as LIVES and the only way I can think to do it is by enabling and disabling the appropriate mesh renderers when the player is at certain health percentages.

I've written this code so far which gives me no errors but it doesn't work the way expected. I haven't yet got a damage functionality working either. so if someone could help me to write an apply damage script to go with this that would be super as well!

if you read the code you may be able to grasp how i'm expecting this code to work but if not do please ask so i can describe how i want this to work..

(i'm not very good at programming either)

 public var Health = 100;
 public var MinHealth = 0;
 public var MaxHealth = 100;
 
 public var Lives = 3;
 public var MinLives = 0;
 public var MaxLives = 3;
 
 public var Damage = 35;//Damage is in increments of 35
 
 
 function Start ()
 {
     Lives = 1;//Player starts with 3 lives
     Health = 30;//Player starts with 100 points of health
 
 }
     
 function Update ()
 {
     if(Health >= 101);//If players health is greater or equal to 101 make it equal 100
     {
         Health = 100;
     }
     
     if(Health == 100);//If players health is equal to 100 go to the following function
     {
         FullHealth();
     }
     
     if(Health == 65);//If players health is equal to 65 go to the following function
     {
         TwoThirdsHealth();
     }
     
     if(Health == 30);//If players health is equal to 30 go to the following function
     {
         OneThirdHealth();
     }
     
     if(Health <= 0);//If players health is equal to 0 go to the following function
     {
         NoHealth();
         Die();
         //minus life
     }
     
     if(Lives >= 4); //If players health is greater or equal to 4 make it equal 3
     {
         Lives = 3;
     }
         
     if(Lives == 3);  //When Player has 3 lives go to the following function
     {
         ThreeLives();
     }
     
     if(Lives == 2);  //When Player has 2 lives go to the following function
     {
         TwoLives();
     }
     
     if(Lives == 1); //When Player has 1 life go to the following function
     {
         OneLife();
     }
     
     if(Lives == 0); //When Player has no lives go to the following functions
     {
         NoLives();
         PermaDeath();
     }
 }
 
 //Health GUI
         
 function FullHealth()//Displays appropriate health icon 
 {
         GameObject.Find("3Health").renderer.enabled = true;
         GameObject.Find("2Health").renderer.enabled = false;
         GameObject.Find("1Health").renderer.enabled = false;
         GameObject.Find("0Health").renderer.enabled = false;
 }
 
 function TwoThirdsHealth()//Displays appropriate health icon
 {
         GameObject.Find("3Health").renderer.enabled = false;
         GameObject.Find("2Health").renderer.enabled = true;
         GameObject.Find("1Health").renderer.enabled = false;
         GameObject.Find("0Health").renderer.enabled = false;
 }
 
 function OneThirdHealth()//Displays appropriate health icon
 {
         GameObject.Find("3Health").renderer.enabled = false;
         GameObject.Find("2Health").renderer.enabled = false;
         GameObject.Find("1Health").renderer.enabled = true;
         GameObject.Find("0Health").renderer.enabled = false;
 }
 
 function NoHealth()//Displays appropriate health icon
 {
         GameObject.Find("3Health").renderer.enabled = false;
         GameObject.Find("2Health").renderer.enabled = false;
         GameObject.Find("1Health").renderer.enabled = false;
         GameObject.Find("0Health").renderer.enabled = true;
 }
 
 //Lives GUI
 
 function ThreeLives()//Displays appropriate life count
 {
         GameObject.Find("0Lives").renderer.enabled = false;
         GameObject.Find("1Life").renderer.enabled = false;
         GameObject.Find("2Lives").renderer.enabled = false;
         GameObject.Find("3Lives").renderer.enabled = true;
 }
 
 function TwoLives()//Displays appropriate life count
 {
         GameObject.Find("0Lives").renderer.enabled = false;
         GameObject.Find("1Life").renderer.enabled = false;
         GameObject.Find("2Lives").renderer.enabled = true;
         GameObject.Find("3Lives").renderer.enabled = false;
 }
 
 function OneLife()//Displays appropriate life count
 {
         GameObject.Find("0Lives").renderer.enabled = false;
         GameObject.Find("1Life").renderer.enabled = true;
         GameObject.Find("2Lives").renderer.enabled = false;
         GameObject.Find("3Lives").renderer.enabled = false;
 }
 
 function NoLives()//Displays appropriate life count
 {
         GameObject.Find("0Lives").renderer.enabled = true;
         GameObject.Find("1Life").renderer.enabled = false;
         GameObject.Find("2Lives").renderer.enabled = false;
         GameObject.Find("3Lives").renderer.enabled = false;
 }
 
 
 function Die()//Kills the player and takes 1 life away and then respawns player at last checkpoint
 {
     Destroy.GameObject.Find("Character");
     //minus one life
     //respawn
 }
 
 function PermaDeath ()//Loads the Game Over Scene after 3 seconds
 {
     yield WaitForSeconds(3);
         Application.LoadLevel("Game_Over");
 }

Any and all help will be greatly appreciated and if someone can get this working then I wish I could pay you for it! for a skilled programmer I bet this is a short task but this has taken me a week!

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

3 Replies

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

Answer by SirBedlam · Nov 14, 2013 at 05:42 PM

I agree that you should probably use GUITextures for this. I've written some example code based on what it looks like you're trying to achieve. I'm sure this could be cleaned up a bit, as I know JS but I wouldn't say I was an expert. So my apologies if it's a little messy. I've added commenting to explain the different behaviors, and I hope I've done this adequately. Please let me know if you have any problems or questions.

 //The GUITexture for the health. Create a GUITexture and assign the full
 //health texture to it
 var healthTexture : GUITexture;
 
 //Then assign the corresponding health textures directly to these slots
 var health0 : Texture2D;
 var health1 : Texture2D;
 var health2 : Texture2D;
 var health3 : Texture2D;
 
 var health = 100; //Starting health
 
 var zeroThirdHealth = 0; //The amount of health that counts as none (I'm assuming it's 0)
 var oneThirdHealth = 30; //The amount of health that counts as 1 third
 var twoThirdHealth = 65; //The amount of health that counts as 2 thirds
 var threeThirdHealth = 100; //The amount of health that counts as 3 thirds
 
 //The GUITexture for the lives. Create a GUITexture and assign the full
 //lives texture to it
 var livesTexture : GUITexture;
 
 //Then assign the corresponding life textures directly to these slots
 var lives0 : Texture2D;
 var lives1 : Texture2D;
 var lives2 : Texture2D;
 var lives3 : Texture2D;
 
 var lives = 3; //Starting lives
 
 //Is there a life 0? In other words, once the life count reaches 0, does the
 //player die, or do they still have one more try?
 var life0 = false;
 
 //You can use this just to test the damage system beforehand. Just click
 //the box in the inspector to call the ApplyDamage function with 35 damage
 var testApplyDamage = false;
 
 function Start () {
 
 }
 
 function Update () {
 
     //Just for testing
     if (testApplyDamage == true)
     {
         testApplyDamage = false;
         ApplyDamage(35);
     }
     
     if (health > threeThirdHealth) //If health gets above the 3 thirds point, bring it back down
     {
         health = threeThirdHealth;
     }
     
     if (health < zeroThirdHealth) //If health gets below the 0 thirds point, bring it back up so it doesn't go negative
     {
         health = zeroThirdHealth;
     }
     
     //Display the appropriate life texture
     if (lives == 3 && livesTexture.texture != lives3)
     {
         livesTexture.texture = lives3;
     }
     
     if (lives == 2 && livesTexture.texture != lives2)
     {
         livesTexture.texture = lives2;
     }
     
     if (lives == 1 && livesTexture.texture != lives1)
     {
         livesTexture.texture = lives1;
     }
     
     if (lives == 0 && livesTexture.texture != lives0)
     {
         livesTexture.texture = lives0;
     }
         
     if (health == zeroThirdHealth && lives == 1 && life0 == false)
     {
         //Player dies
         print ("Player has died");
     }
 }
 
 //Displays the appropriate health icon. Call this function from anywhere to
 //apply damage, and specify the amount of damage to apply.
 //Like this - ApplyDamage(35);
 //If you're calling this function from another script, you'll need to let
 //that script know where this one is. You would just assign the object
 //THIS script is attached to to a GameObject variable slot in the inspector
 //pane for the script calling this function.
 //Like so - *ObjectWithThisScript*.GetComponent("*NameOfThisScriptExactly*").ApplyDamage(35);
 //You would remove the asterisks (*)
 function ApplyDamage(damage : int)
 {
     health -= damage; //Subtract the specified damage from the current health
     
     //If health is at the 3 thirds point, display the assigned 3/3 health GUITexture
     if (health == threeThirdHealth)
     {
         healthTexture.texture = health3;
     }
     
     //If health is equal to or over the 2 thirds point, but is still under
     //the 3 thirds point, display the assigned 2/3 health GUITexture
     if (health >= twoThirdHealth && health < threeThirdHealth)
     {
         healthTexture.texture = health2;
     }
     
     //If health is equal to or over the 1 third point, but is still under
     //the 2 thirds point, display the assigned 1/3 health GUITexture
     if (health >= oneThirdHealth && health < twoThirdHealth)
     {
         healthTexture.texture = health1;
     }
     
     //If health is over the zero thirds point, but still under the one thirds point,
     //display the assigned 0/3 health GUITexture
     if (health > zeroThirdHealth && health < oneThirdHealth)
     {
         healthTexture.texture = health0;
     }
     
     //If health is under or equal to the zero thirds point, but the lives are above
     //1 (either 2 or 3), reset health back to maximum but subtract a life
     if (health <= zeroThirdHealth && lives > 1)
     {
         lives -= 1;
         health = threeThirdHealth;
         healthTexture.texture = health3;
         //Add any sounds/effects for losing a life here
     }
         
     //If health is under or equal to the zero thirds point and the player had 0 lives
     //left, they die. (This is if life0 is set to true)
     if (health <= zeroThirdHealth && lives == 0)
     {
         healthTexture.texture = health0;
         //Player dies
         print ("Player has died");
     }
         
     //If health is under or equal to the zero thirds point, but the player still
     //had 1 life left and life0 is set to true, do the same as usual
     if (health <= zeroThirdHealth && lives == 1 && life0 == true)
     {
         lives -= 1;
         health = threeThirdHealth;
         healthTexture.texture = health3;
         //Add any sounds/effects for losing a life here
     }
         
     //...However, if they had one life left but
     //life 0 is set to false, then they lose that final life and die
     if (health <= zeroThirdHealth && lives == 1 && life0 == false)
     {
         lives -= 1;
         healthTexture.texture = health0;
         //Player dies
         print ("Player has died");
     }
 }
Comment
Add comment · Show 9 · 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 JakeParrott · Nov 14, 2013 at 08:41 PM 0
Share

Thank you for the help. I shall get on with testing this right away and let you know if it works. Thanks again! :)

avatar image JakeParrott · Nov 15, 2013 at 10:38 AM 0
Share

You are a life saver, I wish I could repay you somehow. this code is working how I would like it to and the explanation you have given really helps as I'm not so good at program$$anonymous$$g as you could probably see from my attempt. Thank you ever so much for the assistance! :)

Now to get some enemies in my game and get my player dying and respawning. Could you give me some pointers on how to get that working if you don't $$anonymous$$d? I'd like to have a go at doing it myself first before I ask exactly how to do it.

avatar image SirBedlam · Nov 16, 2013 at 12:54 AM 0
Share

No problem! I have my own share of questions sometimes, so it's always great to help out a fellow developer! As for the enemies and the dying and respawning, that's going to require quite a bit more scripting, so I can give you a few different resources to help you along your way. If you haven't already, you should check out the Asset Store for 3D models and other assets. You can open it inside Unity itself via Window > Asset Store. There's also a very helpful list here on Unity Answers that provides quite a bit of places you can get 3D models from, free and paid. As for the enemy scripts, there's also quite a bit of examples here on Answers and in the forums that you could find with some easy Google searches. Also, Unity has a bunch of tutorials available, and even some example projects you can download to help you get a good idea of how you can do certain things. The Unify Community Wiki also has a lot of pre-made scripts for all kinds of behaviors. You could bookmark those pages if you find them useful. $$anonymous$$y advice would be to take your time and really get the feel of the coding process. If you use a pre-made script and you already know what it does, open it and look through it, just to see how it does it. Doing this really helped me when I was first getting started. Have fun making games, and I wish you the best of luck!

avatar image JakeParrott · Nov 16, 2013 at 12:53 PM 0
Share

Thank you, I shall take your advice. Program$$anonymous$$g is my only downfall really, if it wasn't for my lack of knowledge in the coding region I could develop games on my own. But unfortunately it just appears to be one of those hurdles I cant get over!

With your script that you have kindly given me I'm now trying to apply a function that adds health. Would I just reverse the ApplyDamage(35) function you have implemented? as that is what I am attempting now

avatar image JakeParrott · Nov 16, 2013 at 12:56 PM 0
Share

alt text

Here is an image of the health bar you have kindly helped me create. Hopefully at the end of this I shall have a nice template for making 2.5d games which I hope to share... (it is far from done though)

capture.jpg (73.0 kB)
Show more comments
avatar image
0

Answer by gajdot · Nov 14, 2013 at 04:10 PM

This is way too overkill and overcomplicated. What you need is a GUITexture set up and a script with a link to this GUITexture, a public health variable, a function to damage it and script to handle health and an array of textures. What you can do to simplify is that divide your health with a number to get proportions, for example health%25 will give you back 0,1,2,3,4, which you can use to set the appropriate texture in the GUITexture.Texture = textures[dividedValue]; this way you got your gui update up and running in three line of code.

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
avatar image
0

Answer by gajdot · Nov 14, 2013 at 04:10 PM

K, i took the time to write something you want, it wasn't tested, but you need something like this. Put this script on an empty gameobject and hook up the guitexture, textures, and tweak values if you wish.

 public int health = 100;
     public int maxHealth = 100;
     public int minHealth = 0;
     public int lives=3;
 
     public int damage = 35;
 
     public GUITexture gui;
     public Texture[] textures;
 
     private int lastTexture;
     // Use this for initialization
     void Start () {
     
     }
 
     public void Damage()
     {
         health-=damage;
     }
 
     // Update is called once per frame
     void Update () {
         if(health<0) health =0;
         if(health>100) health = 100;
 
         int currentTexture = health % 25;
         if (lastTexture!=currentTexture)
         {
             lastTexture=currentTexture;
             gui.texture = textures[currentTexture];
         }
 
         if(health == 0)
         {
             Destroy(GameObject.Find("Character"));
             health = 100;
             --lives;
             //do respawn
         }
         if(lives == 0 )
         {
             Application.LoadLevel("Game_Over");
         }
     }
Comment
Add comment · Show 6 · 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 JakeParrott · Nov 14, 2013 at 08:41 PM 0
Share

Thank you for the help. I shall get on with testing this right away and let you know if it works. Thanks again! :)

avatar image JakeParrott · Nov 15, 2013 at 10:34 AM 0
Share

Unfortunately I couldn't get this code working, it gave me a few errors of which I couldn't understand due to my lack of knowledge. If I understood what the problem was I would more than likely be able to fix it. But Thank you ever so much for helping me out, much appreciated :)

avatar image gajdot · Nov 15, 2013 at 10:36 AM 0
Share

Are you using javascript? :D This code I wrote is in c sharp... Didn't noticed it, you just need to convert it to javascript.

avatar image JakeParrott · Nov 15, 2013 at 10:47 AM 0
Share

ah yes silly me! I can see it now. The Goliath's script below is working perfectly for me though so I will more than likely be using that one. But I shall still have a go at using the code you have provided here and let you know if it works or not. its the least I can do for the help! :)

avatar image gajdot · Nov 15, 2013 at 10:57 AM 0
Share

Use witch ever is better for you. If you can understand the other script better you can as well go with it, however it gives a bit of overhead on CPU because of the lot of ifs. But if you are building for PC that shouldn't be a problem. If you want a compact solution use the one I provided, just convert it to javascript :) I believe if you just change the way of declaration it should be working.

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

18 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

Related Questions

Health bar only updates on character death 1 Answer

LoadLevel Health Question. 1 Answer

Can't move GUI 1 Answer

Setting Scroll View Width GUILayout 1 Answer

I loose health after touched enemy and my script only displays the first bloodimage 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