- Home /
Loop fail and unity crash ( eng and spanish )
hi, hola, anyone can explain me why i cant use this condition ? Alguien me puede decir porque no puedo usar esta condicion ?
private void OnTriggerStay(Collider other)
{
if (other.name == "healerTriger")
Healing();
}
void Healing()
{
takingDmg = false;
do
{
hpPlayer = 1 * Time.deltaTime;
}
while (hpPlayer< 50);
}
he probado if y while y sigue petando , pero no veo porque ya que la condicion tiene limite. Even whith if or while the condition crash unity but why ? theres a limit in there.... Big Thz
Answer by NPnpNPnpNP · Jan 27, 2021 at 11:53 AM
You do not need this loop because Healing() will be called every frame. If i understanded what you want to do, this code should work fine.
private void OnTriggerStay(Collider other)
{
if (other.name == "healerTriger")
Healing();
}
void Healing()
{
takingDmg = false;
if (hpPlayer < 50)
hpPlayer = 1 * Time.deltaTime;
}
yep in fact is the same but....we cannot use while loops in unity or what?
Yes you can, but it will do all the loop in one frame, so the health will be fully recovered in one frame. Did you check the log of Unity to know a bit more about the crash?
i get no error log...unity collapses , crash and die.....ctrl alt sup to recover ;P
Ok thz, finally i fix , i have some problem to understand conditions.... i used this and works but like u said...while operates on a frame, even if the operation is *time.deltatime.... luckily if respects the operation
while (takingDmg == false && herido == true)
Healing();
//if (takingDmg==false&&herido==true)
// Healing();
Well, your while loop is an infinite loop. You set hpPlayer to a more or less fixed value which will always be less than 50. Time.deltaTime is a value smaller than 1 which you multiply by 1. So the overall value is less than 1. Since you set your hpPlayer variable to that value, it will never be over 50. What you may wanted to do is
hpPlayer += 1 * Time.deltaTime;
note the difference to
hpPlayer = 1 * Time.deltaTime;
In the first case we are "adding" the amount to the variable while in your case you set the value to that amount.
Though even if you want a while loop that completes within one frame, the usage of deltaTime would not make any sense in that case. deltaTime only makes sense for things you do every frame.