C# Script on Health bar and armor

Somebody can help me to build script in my game… I want to have a health bar and an armor on my character and i do not know to do it in C#… Could somebody Help me… Thanks… please reply…

Well, your going to want to creat a GUI text from gameobject tab, then place it where you want, then create a C# script called Health, add it to the main character or whatever,.
in health code add this

	public static float health;
	public GUIText healthText;

	void Start () 
	{
		healthText = healthText.GetComponent<GUIText>();
		health = 100f;
	}
	
	void Update () 
	{
		healthText.text = health.ToString();
	}

if you want a health bar, look here:

now go into where ever you added the script to, and add the guitext that you create to the healthtext box
There you go, the basics for health, this will just make it display your health, if you want it for armor, pretty much the exact same thing,
it would look like this with armor

	public static float health;
    public static float armor
    public GUIText armorText;
	public GUIText healthText;

	void Start () 
	{
		healthText = healthText.GetComponent<GUIText>();
        armorText = armorText.GetComponent<GUIText>();
		health = 100f;
        armor = 100f;
	}
	
	void Update () 
	{
		healthText.text = health.ToString();
        armorText.text = health.ToString();
	}

We make armor and health a static because we dont really need to modify health in the inspector, also we would want other scripts to modify this such as an enemy script,

to make a basic take damage script, set your player as the tag player, make sure the health script we made before is attached, then create a cube, create a new script called “HealthTest” and inside do something like

public int collisonDamage = 1;
void Update()
 {

    if (colliderHit.tag == "Player")
	{
		Health.health -= collisonDamage;
	}

  }

Sorry if it isn’t organised code, I am writing this in the reply, and tabs won’t work :confused:

Now, I haven’t tested armor yet, so this might not work,but maybe something like

public int collisonDamage = 1;
void Update()
 {

    if (colliderHit.tag == "Player")
	{
             if(Health.armor > 0)
              {
                  Health.armor -= collisonDamage;
              }
             else
             {
                  Health.health -= collisonDamage;
             }
	}

  }

if you wanted a more complex armor, like it takes a fraction of the damage, just change it to something like this:

public int collisonDamage = 10;
public float healthDamagePercent = 5;
public float armorDamagePercent = 1.25;

void Update()
 {

    if (colliderHit.tag == "Player")
	{
             if(Health.armor > 0)
              {
                  Health.armor -= collisonDamage / armorDamagePercent;
                  Health.health -= collisonDamage / healthDamagePercent;
              }
             else
             {
                  Health.health -= collisonDamage;
             }
	}

  }

So what does this do?
Well, first we are creating the health as a float, so we can support decimal places, and making it public static, we wont be able to edit it in the inspector, but we shouldn’t need to, we make it public static for later scripts so they can access it, ease of access, then we are setting the attached gui text’s to our health.

then we are seeing if the cube is touching the tag “Player”, so on our player, if we set the tag to Player, when you collide with the cube, it will detect you as a Player, and execute the following code

What are we executing then? well, when we touch it, it checks if we have armor, if we do, then it knows to take armor instead of health, or to divide a fraction of the damage to health and armor, the first example, makes it so the health will not go down unless armor is < 0, so we only loose armor, but I have seen some games divide it, so i did 2 ways, a more complex way where the majority comes of armor, but a bit of health, for that we need to make it so what ever we divide for health and armor, both complete the collison damage, example, 10 / 1.25 is 8, and 10 / 5 is 2, 8 + 2 = 10, the original number, so these two numbers(1.25, 5), work, so what happens is, say if the collison damage is 10, 8 armor will go and 2 health.

I’m also quite new to c# and unity, but if you have any questions, I’ll try my best to help :slight_smile:
Good luck! :slight_smile:

the the colliderHitis another class sir?

I know it’s kinda late but…
you can check out my video on how to do this on jewtube:

1 Like