Answer the question
In order to leave comments, you need to log in
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);
}
}
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;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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);
}
private void OnEnable()
{
GetComponent<Health>().OnChange += OnChange;
}
private void OnDisable()
{
GetComponent<Health>().OnChange -= OnChange;
}
private void OnChange(int hitpoints, int amount)
{
//здесь уже делай всю визуальную часть, основанную на значениях выше.
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question