How do I get a Switch Statement to work with a For Loop?

I have a character that has 3 lives and no lifebar, but when he reaches 5,000 points and collects a megakey he can transform to a supermode. Once he transforms into supermode his regular one hit goes away and is replaced with lifebars. Each life bar is worth 5,000 points and at full power is 14 lifebars and 70,000 points.
Once the character has at least 5000 points and collects a megakey and transforms those points are then subtracted and converted to one life bar.

I would the to amount of points he has appear as a lifebar once the key is collected and he converts to show on the UI.

here is the code.

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

public class GameManager : MonoBehaviour
{

    private HealthBar healthBar;
   
    public LevelManager levelManager;
    public _AmagonController amagonController;
    public Animator mygoSign;
    public AudioSource MegagonTheme;
    public AudioSource MegagonMorphSound;
    public AudioSource LandingSound;
    public AudioSource Level1Music;
    public AudioSource ItemCollect;
    public AudioSource AmagonDeath;
    public AudioSource enemyshotSound;
    public AudioSource endLevel;
    public Canvas canvas;

    public Image gameoverTryAgainImage;
    public Text tryAgainText;
    public Text gameoverText;
    public Image Points;
    public Text pointText;
    public Text ptsText;


    public Image Bullets;
    public Image bulletIcon;
    public Text bulletText;

    public GameObject megagonLifeHolder;
    public int megagonHealth = 100;
    public Image lifeBarImage;
    public Sprite full;// converted from image to sprite
    public Sprite empty; // converted from image to sprite

   


    public Image megagonLifeIcon;
    public int megagonTotallifebars = 14;
    public int megagoncurrenthealth;
    public int damageToTake = 1;
    public int lifeToAdd = 1;

  



    public Image goSign;
    public Image Lives;
    public Image LivesIcon;
    public Text livesText;


    public int bulletCount = 300;
    public int bulletToAdd = 20;
    public int pointCount = 0000000;

    public int bonusPointForLife = 5000;

   

    public int lifecount = 3;

    // Use this for initialization
    void Start()
    {

        healthBar = FindObjectOfType<HealthBar>();
        levelManager = GetComponent<LevelManager>();
       

        amagonController = GetComponent<_AmagonController>();

      
    }



    // Update is called once per frame
    void Update()
    {
       
    }

 

    /*
     public Image lifeBarImage;
     public Sprite full;// converted from image to sprite
     public Sprite empty; // converted from image to sprite
     */

    public void BonusPointForLife()
    {
        if (pointCount >= bonusPointForLife)
        {
            megagoncurrenthealth += 1;
            pointCount -= bonusPointForLife; //we are taking away points after megagon life point added
            megagoncurrenthealth = megagonTotallifebars;
            pointText.text = "000" + pointCount;

            healthBar.TakeDamage(damageToTake);
            healthBar.UpdateLifeBars();
        




        }
    }


    public void HurtMegagon(int dagmageToTake) // taking away life bar as Megagon
    {
       megagoncurrenthealth -= damageToTake;
    }

    public void AddLifebarstoMegagon(int lifeToAdd)  // adding  life bar as Megagon
    {
        megagoncurrenthealth += lifeToAdd;
    }

    //updated switch statement instead of turning off and one the group
    // i switched full powerbar to empty it's a bit different but it works better
    public void MegagonLifeBarAdder()
    {

    }
     





}





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

public class Switch : MonoBehaviour {
    public GameManager gameManager;
    public int megagonHealthBars = 14;
    public HealthBar healthBar;
   

    void Start()
    {
       
        gameManager = FindObjectOfType<GameManager>();
        healthBar = FindObjectOfType<HealthBar>();
    }
    // Update is called once per frame
    void Update ()
    {
        MegagonHealthBarsAdd();

    }

    public void MegagonHealthBarsAdd()
    {

        switch (megagonHealthBars)
        {
            case 13:
                if (gameManager.bonusPointForLife >= 70000)
                {
                    print("We have all  14 lifebars showing.");
                }
                break;
            case 12:
                if (gameManager.bonusPointForLife >= 65000)
                {
                    print("We have  13 lifebars showing.");
                }
                break;
            case 11:
                if (gameManager.bonusPointForLife >= 60000)
                {
                    print("We have  12 lifebars showing.");
                }
                break;
            case 10:
                if(gameManager.bonusPointForLife >= 55000)
                { 
                print("We have  11 lifebars showing.");
                }
                break;
            case 9:
                if (gameManager.bonusPointForLife >= 50000)
                { 
                    print("We have  10 lifebars showing.");
                }
                break;
            case 8:
                if (gameManager.bonusPointForLife >= 45000)
                { 
                    print("We have  9 lifebars showing.");
                }
                break;
            case 7:
                if (gameManager.bonusPointForLife >= 40000)
                { 
                    print("We have  8 lifebars showing.");
                }
                break;
            case 6:
                if (gameManager.bonusPointForLife >= 35000)
                {
                    print("We have  7 lifebars showing.");
                }
                break;
            case 5:
                if (gameManager.bonusPointForLife >= 30000)
                {
                    print("We have  6 lifebars showing.");
                }
                break;
            case 4:
                if (gameManager.bonusPointForLife >= 25000)
                {
                    print("We have  5 lifebars showing.");
                }
                break;
            case 3:
                if (gameManager.bonusPointForLife >= 20000)
                {
                    print("We have  4 lifebars showing.");
                }
                break;
            case 2:
                if (gameManager.bonusPointForLife >=15000)
                {
                    print("We have  3 lifebars showing.");
                }
                break;
            case 1:
                if (gameManager.bonusPointForLife >= 10000)
                {

                    print("We have  2 lifebars showing & 5000pts.");
                }
                break;
            case 0:
                if (gameManager.bonusPointForLife >= 5000)
                {

                    print("We have  1 lifebar showing.");
                }
                break;

            default:
                print("No Lifebar showing.");
                break;


        }

    }

}





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

public class HealthBar : MonoBehaviour
{
    // here i make a new class to hold a some data about one health bar
    [Serializable] // this attribute just allows it to show in the inspector
    public class LifeBar
    {
        public GameObject megagonLifeHolder;
        public Image lifeBarImage; // component to show a sprite
        public Sprite full;
        public Sprite empty;

        // convenience functions to control the image
       
        public void setFull()
        {
            lifeBarImage.sprite = full;
        }

        public void setEmpty()
        {
            lifeBarImage.sprite = empty;
        }

        public void show()
        {
            megagonLifeHolder.gameObject.SetActive(true);
            lifeBarImage.enabled = true;
            print("we are enablinglifeholder");


        }

        public void hide()
        {
            lifeBarImage.enabled = false;
            megagonLifeHolder.gameObject.SetActive(false);
            print("we are disabling lifeholder");


        }
    }

    // here is a list of the class above
    public List<LifeBar> lifeBars; // set this up in the inspector

    private int currentLife; // keep track of the player's life

    public void Start()
    {
        // show all life bars using a foreach loop
        foreach (LifeBar lifeBar in lifeBars)
        {
            lifeBar.show();
            //lifeBar.hide();
        }

        // start off with full life
        currentLife = lifeBars.Count;

        UpdateLifeBars();



        //////////////
 
}

    public void TakeDamage(int damage)
    {
        // add damage and keep currentLife above 0
        currentLife = Mathf.Max(0, currentLife + damage);

        UpdateLifeBars();
    }

    public void UpdateLifeBars()
    {
        // using a for loop, show or hide each life bar depending on "currentLife"
        for (int i = 0; i < currentLife; i++)
        {
            if (i < currentLife)
            {
                lifeBars[i].setFull();  //.show();
            }
            else
            {
                lifeBars[i].setEmpty(); //.hide();
            }
        }
    }
}

it looks like you need to add the UI text element to the health bar to display the points.
Then just update the text with the points when ever the points change. UItext.text = pointCount.ToString();

In this loop:

        // using a for loop, show or hide each life bar depending on "currentLife"
        for (int i = 0; i < currentLife; i++)
        {
            if (i < currentLife)
            {
                lifeBars[i].setFull();  //.show();
            }
            // CODE BELOW WILL NEVER RUN
            else
            {
                lifeBars[i].setEmpty(); //.hide();
            }
        }

int i will never be >= currentLife.

You should loop over all bars available (lifeBars.Count) instead of just bars illuminated.

Completely random thought here and unrelated to your problem, but I’ll just say it’s typically not a good idea to use keywords for your variable/class/etc. names. In this case you’re using “Switch”, which is uppercase, and therefore different than “switch” (so it’s unique), but eventually someone that may have to work on one of your projects (should this ever happen) will potentially get confused by doing something like this. Call it something more meaningful… HealthbarSwitch or HealthbarController or something that makes sense. Anywho… back to solving your issue, sorry to side track the convo!

1 Like