Home > database >  How to increase the number of lives over time?
How to increase the number of lives over time?

Time:05-17

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?

CodePudding user response:

You're calling

lives  = Mathf.RoundToInt(Time.deltaTime * time);

but Time.deltaTime is the time between frame draws. Even an awful game might still have 10 fps, which means Time.deltaTime is 0.1 seconds. You're passing 1 in as the argument to that function, so (0.1*1) = 0.1.

Then you take 0.1, round it to an integer, so it rounds to 0. Then you increment lives by zero.

I really can't tell what the goal is here with the time dependency on adding lives, so unfortunately I've got no suggestions, but hopefully this helps.

CodePudding user response:

An easy way to ask your question is to use InvokeRepeating, which works as follows:

public void Start()
{
    InvokeRepeating(nameof(AddLife), 0f, 2f);
}

public void AddLife() // add 1 life every 2 sec
{
    life = Mathf.Max(  life, maxLives);
    Debug.Log("Life: " life);
}

If you want to be time dependent, change the life adjustment algorithm to Time.deltaTime to Time.time.

public void Update()
{
    SetLife(3f); // set life every 3 sec for e.g.
}
public void setLives(float repeatTime) 
{
    life = (int) (Time.time / repeatTime);
}
  • Related