enemy bar link togter

enemy bar link together

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

public class enemyhealing : MonoBehaviour
{
    bulletmovement howgetdamge;
    public HealthBar healthbar;
    enemy thisenemy;

    void Start()
    {
        thisenemy = FindObjectOfType<enemy>();
        healthbar.SetUp(thisenemy.EnemyHealth);
    }

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

this is the bar

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

public class HealthBar : MonoBehaviour
{
    private healthSystem HealthSystem;

    public void SetUp (healthSystem HealthSystem)
    {
        this.HealthSystem = HealthSystem;
    }
    private void Update()
    {
        transform.Find("Bar").localScale = new Vector3(HealthSystem.GetHealthPercent()*10, 1);
    }
}

how I spread the enemy bars every time I do damage to one of tham all the health bar of all the enemy go down

@eyalgruber

You could have your enemy have reference to your health bar.

And each enemy should have its own health bar, unless you want to have some special setup where you show only one health bar…

When you damage the enemy, enemy’s own damage method should “know” about the bar and update it.

P.S.
You might consider correcting the post heading, and making the question a bit more clear, now I just guessed what you are trying to do (= you seem to have a problem of all health bars updating when only one health bar should update).

Something like this could work:

public class Enemy : MonoBehaviour
{
    [SerializeField] HealthBar healthBar;
    [SerializeField] int health = 100;

    void Start()
    {
        healthBar = GetComponent<HealthBar>();
    }


    public void DamageEnemy(int amount)
    {
        health -= amount;

        Debug.Log("Health:" + health);

        // update heath bar method gets called in health bar and updates visuals.
        healthBar.UpdateValue(health, 100);
    }
}
1 Like