Home > Blockchain >  Unity 2D - How can I set the value of instantiated sliders for each character health in a different
Unity 2D - How can I set the value of instantiated sliders for each character health in a different

Time:10-23

I am working on a 2D game and I am creating a health system. I have it all set up, now I want to be able to update the slider values to show what each characters health level is at. Note the health bars are not child objects of each character. But rather part of the GameManager UI object. I am setting it this way because the player can choose up to 4 characters to bring into the battle scene.

In this block below you can see that in the awake function I am getting a list of the characters in the scene and instantiating a health bar prefab object for each one.

The question is: how can I update each slider to match the value for each character?

private void Awake() 
{
        characters = GameObject.FindGameObjectsWithTag("Draggable");
        foreach (GameObject character in characters)
        {
            var location = GameObject.Find("Character Health Window");
            GameObject healthBar = Instantiate(healthBarPrefab, location.transform);
            healthBar.GetComponent<HealthSlider>().characterName.text = character.name;

        listOfHealthBars.Add(healthBar);
        listOfCharacters.Add(character);
    }
}

private void Update() 
{
    foreach (GameObject character in listOfCharacters)
    {
        foreach (GameObject healthBar in listOfHealthBars)
        {
            healthBar.GetComponent<HealthSlider>().slider.value = character.GetComponent<CharacterHealthAndLevel>().currentHealth;
            healthBar.GetComponent<HealthSlider>().slider.maxValue = character.GetComponent<CharacterHealthAndLevel>().maxHealth;
        }
    }
}

Here you can see that I have two characters in game, so two health bar objects are being instantiated. Then they took damage so their health is at 50 (max is 100). How can I update these sliders, when they are not attached to the characters? But instead an instantiated prefab of the GameManager script?

Inspector Setting In Game

Edit: I have gotten the health bars to instantiate correctly, with the correct name next to the health bar. But they will both update at the same time. They are both reading the value from the first instance. How do I separate it out so each bar is tied to the correct character?

CodePudding user response:

In your GameManager script you have to store a reference to your characters and your health bars. Then in the update method you have to update the health bars accordingly.

So just store a collection of your characters in your GameManager and a collection of health bars.

A way of implementing would be to keep a Dictionary for those values:

Dictionary<GameObject, HealthSlider> characterHealthBars = new Dictionary<GameObject, HealthSlider>();

For adding characters with a health bar:

characterHealthBars.Add(yourCharacterGameObject, yourHealthSlider);

For updating:

foreach (var item in characterHealthBars) {
    // update health slider 
    // item.Key is the character object, item.Value is the health slider
}

More information about dictionaries in c# here

  • Related