public void UpdateMaxHealth() { Image[] lives = LivesContainer.GetComponentsInChildren <Image>(); int maxPlayerHealth = player.GetMaxHealth(); // If we have more children than max health, then we need to remove if (LivesCount > maxPlayerHealth) { int diff = LivesCount - maxPlayerHealth; for (int i = 0; i < LivesCount - maxPlayerHealth; ++i) { Destroy(lives[LivesCount - 1].gameObject); LivesCount--; } } else // Else, we need to add health { int diff = maxPlayerHealth - LivesCount; for (int i = 0; i < diff; ++i) { AddHealth(true); LivesCount++; } } }
public void UpdateHealth() { Image[] lives = LivesContainer.GetComponentsInChildren <Image>(); int curPlayerHealth = player.GetHealth(); int UIFullHealthCount = 0; for (int i = 0; i < lives.Length; ++i) { if (lives[i].sprite.Equals(FullHealthSprite)) { UIFullHealthCount++; } } // If player health is more than we have set, we need to add if (curPlayerHealth > UIFullHealthCount) { for (int i = UIFullHealthCount; i < curPlayerHealth; ++i) { lives[i].sprite = FullHealthSprite; } } else // Else, we need to remove { for (int i = UIFullHealthCount; i > curPlayerHealth; --i) { lives[i - 1].sprite = EmptyHealthSprite; } } }