#Maybe Infinite loop

1 messages · Page 1 of 1 (latest)

formal hound
#

I added this code to replenish and use stamina but it wont work and when i play the game unity seems to crash. i dont know if its unity or an infinite loop because it just freezes up before i can test the game.

i will link my code below but the important part is the Sprint method.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameInput : MonoBehaviour
{
    public float moveSpeed;
    public float clockwise = 50f;
    public float sprint = 2;
    public float sprintCoolDown = 5;
    public bool isSprinting;


    Player player = new Player();

    [SerializeField] private StaminaBar staminaBar;

    public float maxStamina = 100;
    public float currentStamina;

    public void Sprint()
    {
        currentStamina = maxStamina;
        staminaBar.SetMaxStamina(maxStamina);

        bool hasStamina = true;

        if(currentStamina <= 0)
        {
            hasStamina = false;
        }

        while (currentStamina > 0)
        {
            moveSpeed = moveSpeed * sprint;

            staminaBar.UseStamina(1);
            isSprinting = true;
        }

        if (hasStamina == false || isSprinting == false)
        {
            WaitSeconds();
            while (currentStamina < 50 || isSprinting == false)
            {
               currentStamina = currentStamina +1;
            }
            hasStamina = true;
        }
    }

    IEnumerator WaitSeconds()
    {
        yield return new WaitForSeconds(sprintCoolDown);
    }
}```
#

this is the code with the staminaBar Methods:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StaminaBar : MonoBehaviour
{
    public float maxStamina = 100;
    public float currentStamina;

    public Slider slider;

    public void SetMaxStamina(float stamina)
    {
        slider.maxValue = stamina;
        slider.value = stamina;
    }

    public void SetStamina(float stamina)
    {
        slider.value = stamina;
    }

    public void UseStamina(float staminaUsage)
    {
        currentStamina -= staminaUsage;
    }
}```
soft dust
#
        while (currentStamina > 0)
        {
            moveSpeed = moveSpeed * sprint;

            staminaBar.UseStamina(1);
            isSprinting = true;
        }

yes, this is an infinite loop

#

nothing is changing currentStamina

#

so it'll always be > 0

formal hound
soft dust
#

how would it

formal hound
#
public void UseStamina(float staminaUsage)
    {
        currentStamina -= staminaUsage;
    }```
soft dust
#

That's a different field

#

the currentStamina in StaminaBar is not the same as the one in GameInput

formal hound
#

how could i change it so that it is (other than just not using the method which might be fine anyways)

soft dust
#

your stamina bar (assuming it's UI) should reference the object which tracks stamina

#

and just set the slider value accordingly

formal hound
#

so i need to rewrite the method in gameinput or just say -1

soft dust
#

stamina logic should be done by the thing that has the stamina. presumably the player

#

the UI should not track stamina, or subtract its value

#

the UI should only reflect the current value

#

update it wherever it needs updating. StaminaBar class just sets the slider value, and that's all

formal hound
soft dust
#

a player object

#

the main player behaviour

formal hound
#

yea i might do that later actually

#

and put it in player

#

i was hoping on just havign an upadte method with methods put into it

#

ok so now it didnt crash straight away but it crashed when pressign shoft imma go try fix this

soft dust
#

cool 👍

formal hound
#

ok so now i managed to stop the infinte loop but now i get 2 errors i have never seen :Z

soft dust
#

what errors?

formal hound
#

i think its that my player is zooming away maybe i forgot to do a Time.deltatime; imma check now

#

Screen position out of view frustum (screen pos 419.000000, 206.000000) (Camera rect 0 0 874 756)
UnityEngine.SendMouseEvents:DoSendMouseEvents (int) this error

and Invalid worldAABB. Object is too large or too far away from the origin.

#

tbh i cant tell where i could change it other than just maybe saying that movespeed is 5 everytime so that it resets :Z