A
A
Azmi2021-11-28 13:28:23
Unity
Azmi, 2021-11-28 13:28:23

How to make sprites not change in all duplicates at once?

I created three duplicates of the same enemy, but when I hit one of them, the HP sprite changes for everyone, although only the one I hit decreases the HP. How can I make sure that the xp sprites do not change for the rest? Most likely it's easy, but I either don't see or don't understand something

Visually - https://www.youtube.com/watch?v=jHu9Y8JMSb4

Enemy

private void Awake()
    {
        health = currentHealth.initialValue;
    }
    private void TakeDamage(float damage)
    {
        health -= damage;
        currentHealth.RuntimeValue = health;
        EnemyHealthSignal.Raise();
        if (health <= 0)
        {
            DeathEffect();
            MakeLoot1();
            MakeLoot2();
            MakeLoot3();
            // if(roomSignal != null){
            //  roomSignal.Raise();
            // }
            this.gameObject.SetActive(false);
        }
    }


EnemyHeartManager
public class EnemyHeartManager : MonoBehaviour
{
    public SpriteRenderer[] hearts;
    public Sprite fullHeart;
    public Sprite halfFullHeart;
    public Sprite emptyHeart;
    public FloatValue EnemyHeartContainer;
    public FloatValue EnemyCurrentHealth;

    void Start()
    {
        InitHearts();
    }

    public void InitHearts()
    {
        for (int i = 0; i < EnemyHeartContainer.initialValue; i++)
        {
            hearts[i].gameObject.SetActive(true);
            hearts[i].GetComponent<SpriteRenderer>().sprite = fullHeart;
        }
    }

    public void UpdateHearts()
    {
        float tempHealth = EnemyCurrentHealth.RuntimeValue / 2;
        for (int i = 0; i < EnemyHeartContainer.initialValue; i++)
        {
            if (i <= tempHealth - 1)
            {
                hearts[i].GetComponent<SpriteRenderer>().sprite = fullHeart;
            }
            else if (i >= tempHealth)
            {
                hearts[i].GetComponent<SpriteRenderer>().sprite = emptyHeart;
            }
            else
            {
                hearts[i].GetComponent<SpriteRenderer>().sprite = halfFullHeart;
            }
        }
    }
}


61a35981269cd321509477.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Ente, 2021-11-28
@Ente

There should be no managers. Make two scripts, one named Health.cs and the other HealthView.cs. Both hang on the enemy. In Health, make a health field, a change function and an event.
Health.cs

[SerializedField] private int hitpoints = 100;
public Action<int, int> OnChange;

public void Change(int amount)
{
           hitpoints += amount;
           OnChange?.Invoke(hitpoints, amount);
}

HealthView.cs
private void OnEnable()
{
    GetComponent<Health>().OnChange += OnChange;
}
private void OnDisable()
{
   GetComponent<Health>().OnChange -= OnChange;
}

private void OnChange(int hitpoints, int amount)
{
    //здесь уже делай всю визуальную часть, основанную на значениях выше.
}

If there is a healing later, this script will also help, since you can pass a positive value for healing, a negative value for hitting. To check death, also start a separate script and also subscribe to it, as in the case of HealthView, similarly for dropping loot. Each unique action is a separate script and let them be signed, so they can be easily added and removed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question