Working health script?

Hi I have this script that I have from another question that I asked and was wondering how to make the health bar smaller when I get hit by tagged bullets

public float maxHealth = 100.0f; // Minimum health is 0.0f (dead) public float currentHealth = 100.0f; // Players current health

public Texture2D background = null;

public Texture2D energybar = null;

void OnGUI() {

// Draw the background

GUI.DrawTexture(new Rect(32.0f, 32.0f, 128.0f, 16.0f), background);

// Draw the health/energy bar

GUI.BeginGroup(new Rect(34.0f, 34.0f, 124.0f * (currentHealth / maxHealth), 12.0f)); GUI.DrawTexture(new Rect(0.0f, 0.0f, 124.0f, 12.0f), energybar); GUI.EndGroup(); }

more ▼

asked Dec 31, 2010 at 08:41 PM

avatar image

Phil 9
35 5 6 12

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

What do you mean by smaller? Do you mean that the GUI should be smaller? Try editing the last two parameters in the Rect's.

If you mean that when I get hit, I receive 10% of my life, so show only 90%. Then you just need to subtract currentHealth with the amount you lost.

more ▼

answered Jan 01, 2011 at 12:17 AM

avatar image

2.2k 7 11 26

Yeah that is what I mean but I forgot o put that I have a script but It won't work but I can't post it because I'm on my iPod. So yeah I know what you mean but when I tried it, it didn't work.(i didn't have any GUI on it though)

Jan 01, 2011 at 06:36 AM Phil 9

You need to have two textures for it to work. I know it, because that script looks a lot like one I provided a few days ago :)

Jan 01, 2011 at 11:32 AM Ejlersen
(comments are locked)
10|3000 characters needed characters left

This is easier. Try this.

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
     public class PlayerHealth : MonoBehaviour 
         public float maxHealth; //Your players max Health
         public float curHealth;  //Your players current health
         public Scrollbar healthBar;  //Unity UI Scrollbar Image named healthBar
     void Update()
         //Call HealthBar function, pass it a 0 to avoid object reference error
        //The HealthBar function
         public void HealthBar(float adj) 
         //Will create a variable named percentile based on the current health divided by the max health multiplied by 100.
         float healthPercentile = ((float)curHealth / (float)maxHealth) * 100f; 
 //Will rescale a scrollbar set to FILL type by the percentile variable multiplied by 100.
         healthBar.size = healthPercentile / 100f; 

more ▼

answered Jan 10 at 02:27 AM

avatar image

160 3 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 31, 2010 at 08:41 PM

Seen: 5479 times

Last Updated: Jan 10 at 02:27 AM