I have such a script:
public class SystemLives : MonoBehaviour
{
public int lives;
public int maxLives;
public Image[] Live;
public Sprite FullHearts;
public Sprite EmptyHearts;
public void setLives(int time)
{
lives += Mathf.RoundToInt(Time.deltaTime * time);
if (lives > maxLives)
{
lives = maxLives;
}
for (int i = 0; i < Live.Length; i++)
{
if (i < lives)
{
Live[i].sprite = FullHearts;
}
else
{
Live[i].sprite = EmptyHearts;
}
}
}
public void TakeHit(int damage)
{
lives -= damage;
if (lives <= 0)
{
Debug.Log("Игра окончена");
}
for (int i = 0; i < Live.Length; i++)
{
if (i < Mathf.RoundToInt(lives))
{
Live[i].sprite = FullHearts;
}
else
{
Live[i].sprite = EmptyHearts;
}
}
}
n which there are functions for subtracting and adding life, I hung this script on an empty element, I call it like this:
public GameObject ScriptLives;
private SystemLives systemLives;
public int times=1;
public void Btn()
{
foreach (var s in strings)
{
if (s.Compare() == true)
{
b++;
}
else
{
prob++;
}
}
systemLives = ScriptLives.GetComponent<SystemLives>();
systemLives.setLives(times);
systemLives = ScriptLives.GetComponent<SystemLives>();
systemLives.TakeHit(prob);
Debug.Log($"{b}");
}
I call it like this: It turns out to take lives, but for some reason it is not added. maybe the problem is in the first script? please tell me what the reason may be and how it can be fixed?