Additive Shield Problem

Hi everyone,
I am currently working on a simplistic sphere game. I am trying to make a shield ability where if you get a shield powerup your first shield activates, and once the first shield has reached 100% if you get another shield power up your shield 2 starts to activate and once that has reached 100% shield 3 etc. And I want the same in reverse.

Here is my code:

using UnityEngine;
using System.Collections;
using Image=UnityEngine.UI.Image;

public class PlayerHealth : MonoBehaviour 
{
	bool a1;
	bool a2;
	bool a3;
	bool a4;
	bool a5;
	public Image armour1;
	public Image armour2;
	public Image armour3;
	public Image armour4;
	public Image armour5;
	public float currentHealth;
	public float armour2Health;
	public float armour3Health;
	public float armour4Health;
	public float armour5Health;
	public bool getPowerUp;
	public float powerUpValue;

	// Use this for initialization
	void Start () 
	{
		armour1.fillAmount = currentHealth;
		armour2.fillAmount = armour2Health;
		armour3.fillAmount = armour3Health;
		armour4.fillAmount = armour4Health;
		armour5.fillAmount = armour5Health;

		a1 = true;
		a2 = false;
		a3 = false;
		a4 = false;
		a5 = false;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(getPowerUp == true)
		{

			if (a1 == true)
			{
				Debug.Log("a1 is true");

				if(currentHealth > 1.0f)
				{
					armour1.fillAmount = currentHealth + powerUpValue;
					getPowerUp = false;
				}

				else if(currentHealth <= 1.0f)
				{
					Invoke("lvl1tolvl2", 0.0f);
					getPowerUp = false;
					
					a1 = false;
					a2 = true;
				}
			}
		
			if (a2 == true)
			{
				Debug.Log("a2 is true");

				if(armour2Health > 1.0f)
				{
					armour2.fillAmount = armour2Health + powerUpValue;
					getPowerUp = false;
				}

				else if(armour2Health <= 1.0f)
				{
					Invoke("lvl2tolvl3", 0.0f);
					getPowerUp = false;

					a2 = false;
					a3 = true;
				}
			}
			
			if (a3 == true)
			{
				Debug.Log("a3 is true");

				if(armour3Health > 1.0f)
				{
					armour3.fillAmount = armour3Health + powerUpValue;
					getPowerUp = false;
				}

				else if(armour3Health <= 1.0f)
				{
					Invoke("lvl3tolvl4", 0.0f);
					getPowerUp = false;

					a3 = false;
					a4 = true;
				}
			}
			
			if(a4 == true)
			{
				Debug.Log("a4 is true");

				if(armour4Health > 1.0f)
				{
					armour4.fillAmount = armour4Health + powerUpValue;
					getPowerUp = false;
				}

				else if(armour4Health <= 1.0f)
				{
					Invoke("lvl4tolvl5", 0.0f);
					getPowerUp = false;

					a4 = false;
					a5 = true;
				}
			}
			
			if(a5 == true)
			{
				Debug.Log("a5 is true");
				
				if(armour5Health > 1.0f)
				{
					armour5.fillAmount = armour5Health + powerUpValue;
					getPowerUp = false;
				}

				else if(armour5Health <= 1.0f)
				{
					Invoke("lvl5tolvl6", 0.0f);
					getPowerUp = false;
				}
			}
		}
	}

	public void lvl1tolvl2 ()
	{
		armour2Health = armour2Health + powerUpValue;
	}
	public void lvl2tolvl3 ()
	{
		armour3Health = armour3Health + powerUpValue;
	}
	public void lvl3tolvl4 ()
	{
		armour4Health = armour4Health + powerUpValue;
	}
	public void lvl4tolvl5 ()
	{
		armour5Health = armour5Health + powerUpValue;
	}
	public void lvl5tolvl6 ()
	{
		Debug.Log("COMING SOON!");
	}
}

But when I set the powerUpValue to 0.1 and I check getPowerUp in game mode, all of the shields get a value of 0.1 but none of them show.

I hope this all makes sense and thank you in advance.

I really felt the urge to rewrite this, since you won’t get happy in the near future keeping this way of coding up. Just a general rule of thumb. Whenever you do things more than once (ok maybe more than 2 times), think about how to generalise the behaviour and make it extensible. I haven’t tested what I just wrote, but I think it should work. With this you can have as many shields as you want. Call AddPowerUp() when you receive one and ApplyShieldDamage() when you loose shield by an amount. Does not cover damage larger than 1 though. I assumed your shields have a value of 0 to 1. Update will constantly increase the current shield.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class PlayerHealth : MonoBehaviour
{
	[System.Serializable]
	public class Shield{
		public Image image;
		public float health;

		public void SetHealth(float health){
			this.health = health;
			image.fillAmount = health;
		}
	}

	public Shield[] shields = new Shield[5];
	int shieldIndex = 0;

	// Use this for initialization
	void Start () 
	{
		for (int i = 0; i < shields.Length; i++)
			shields _.SetHealth(shields *.health);*_

* }*

* // Update is called once per frame*
* void Update ()*
* {*
* if (shields [shieldIndex].health >= 1)*
* return;*

* shields [shieldIndex].SetHealth(Mathf.Clamp01(shields [shieldIndex].health + powerUpValue));*
* }*

* // Call this to increase or reduce shield capacity*
* public void AddPowerup(){*

* if (shieldIndex >= shields.Length - 1)*
* return;*

* if (shields [shieldIndex].health >= 1.0f)*
* shieldIndex++;*
* }*

* public void ApplyShieldDamage(float damage){*

* if (damage < 0) {*
* Debug.Log (“Negative damage would be healing, right :)”);*
* return;*
* }*

* float newShieldValue = shields [shieldIndex].health - damage;*
* if (newShieldValue > 0)*
* shields [shieldIndex].SetHealth(newShieldValue);*
* else {*
* shieldIndex = shieldIndex > 0 ? shieldIndex - 1 : shieldIndex;*
* shields [shieldIndex].SetHealth(shields [shieldIndex].health - shieldIndex);*
* }*
* }*
}