Search Unity

Issue Coding Stamina Bar

Discussion in 'Scripting' started by BLACKHAWK4547, Sep 21, 2022.

  1. BLACKHAWK4547

    BLACKHAWK4547

    Joined:
    Mar 24, 2020
    Posts:
    6
    I'm Trying to make a stamina bar for a survival game I'm working on. As far as I can tell my code works but I have one small issue that I feel will cause problems if not addressed now.
    I have a scriptable obj for all the players stats so, stamina( being the max stamina) and staminaCurrent (for the current possible stamina that can be regenerated). and then the script that effects the stamina bar and how much is displayed and used at any time. there is a function that passively depletes the stamina and displays this (staminaCurrent) this all works fine the problem is that when I press LeftShit for the first time to run, if the current Stamina is less than the max, the stamina bar shoots up to the max stamina position then decreases from the max. now if I hold shift and let it deplete past the staminaCurrent declared in the scriptable obj then press shift the bar depletes from where it currently is and recharges to staminaCurrent like i want it too.
    so my question is dose anyone know why it shoots up to the top the first time I press shift. thankyou in advance
    also sorry I'm still somewhat new to coding and I'm sure there was a better way to approach what I've done so far but heres the code

    Code (CSharp):
    1. public Slider staminaBar;
    2.    
    3.     private int currentStamina;
    4.  
    5.     public static StaminaBar instance;
    6.  
    7.     private WaitForSeconds regenTick = new WaitForSeconds(0.1f);
    8.     private Coroutine regen;
    9.  
    10.     public bool isDepleated = false;
    11.  
    12.     public PlayerStatsSOBJ objStamina;
    13.  
    14.     private void Awake()
    15.     {
    16.         instance = this;
    17.     }
    18.  
    19.     void Start()
    20.     {
    21.         currentStamina = objStamina.staminaCurrent;
    22.         staminaBar.maxValue = objStamina.staminaCurrent;
    23.         staminaBar.value = objStamina.staminaCurrent;
    24.  
    25.         InvokeRepeating("passiveStaminaDepleation", 1.0f, 1.0f);
    26.     }
    27.    
    28.     public void Update()
    29.     {
    30.         if (currentStamina <= 0)
    31.         {
    32.             isDepleated = true;
    33.             Debug.Log("no Stamina");
    34.         }
    35.         if (currentStamina >= 1 && isDepleated == true)
    36.         {
    37.             isDepleated = false;
    38.             currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
    39.             staminaBar.value = currentStamina;
    40.  
    41.             if (regen != null)
    42.                 StopCoroutine(regen);
    43.  
    44.             regen = StartCoroutine(RegenStamina());
    45.             Debug.Log("Stamina at 1");
    46.         }
    47.     }
    48.  
    49.     public void UserStamina(int amount)
    50.     {
    51.         if (currentStamina - amount >= 0 && isDepleated == false)
    52.         {
    53.             currentStamina -= amount;
    54.             staminaBar.value = currentStamina;
    55.  
    56.             if (regen != null)
    57.                 StopCoroutine(regen);
    58.  
    59.             regen = StartCoroutine(RegenStamina());
    60.         }
    61.      
    62.     }
    63.  
    64.     private IEnumerator RegenStamina()
    65.     {
    66.         yield return new WaitForSeconds(2);
    67.         while(currentStamina < objStamina.staminaCurrent && isDepleated == false)
    68.         {
    69.             currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
    70.             staminaBar.value = currentStamina;
    71.             yield return regenTick;
    72.         }
    73.         if (currentStamina <= 0)
    74.         {
    75.             Debug.Log("Stamina Depleated");
    76.             yield return new WaitForSeconds(2);
    77.             while (currentStamina < objStamina.staminaCurrent && isDepleated == true)
    78.             {
    79.                 currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
    80.                 staminaBar.value = currentStamina;
    81.                 yield return regenTick;
    82.             }
    83.         }
    84.         regen = null;
    85.     }
    86.  
    87.     private void passiveStaminaDepleation()
    88.     {
    89.         if (currentStamina > 10)
    90.         {
    91.             staminaBar.value -= 1;
    92.             objStamina.staminaCurrent -= 1;
    93.         }
    94.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Here is how you can reason about it frame by frame as it runs:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.

    You must find a way to get the information you need in order to reason about what the problem is.
     
    BLACKHAWK4547 and orionsyndrome like this.
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,097
    lol you're a legend
     
    Kurt-Dekker likes this.
  4. Franco_Voisard

    Franco_Voisard

    Joined:
    Apr 30, 2018
    Posts:
    20
    Hi! I tried to simplify a little bit your code to help you find your bug.

    Here it is :) Hope it helps.


    Code (CSharp):
    1. public Slider staminaBar;
    2.  
    3.     private int currentStamina;
    4.     public static StaminaBar instance;
    5.     private WaitForSeconds regenTick = new WaitForSeconds(0.1f);
    6.     private Coroutine regen;
    7.     public bool IsDepleted => currentStamina <= 0;
    8.     public PlayerStatsSOBJ objStamina;
    9.  
    10.     private void Awake()
    11.     {
    12.         instance = this;
    13.     }
    14.  
    15.     private void Start()
    16.     {
    17.         currentStamina = objStamina.staminaCurrent;
    18.         staminaBar.maxValue = objStamina.staminaCurrent;
    19.         UpdateBarValue();
    20.         InvokeRepeating(nameof(DepleteStaminaPassively), 1.0f, 1.0f);
    21.     }
    22.  
    23.     public void Update()
    24.     {
    25.         if (currentStamina >= 1 && IsDepleted)
    26.         {
    27.             //Not sure what you are trying to achieve.
    28.             //(objStamina.staminaCurrent / objStamina.staminaCurrent will always be 1.)
    29.             currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
    30.             UpdateBarValue();
    31.             RegenerateStamina();
    32.         }
    33.     }
    34.  
    35.     public void UserStamina(int amount)
    36.     {
    37.         var subtractionIsPositive = currentStamina - amount < 0;
    38.         if (subtractionIsPositive || IsDepleted) return;
    39.      
    40.         currentStamina -= amount;
    41.         UpdateBarValue();
    42.         RegenerateStamina();
    43.     }
    44.  
    45.     private void RegenerateStamina()
    46.     {
    47.         if (regen == null) regen = StartCoroutine(RegenStamina());
    48.     }
    49.  
    50.     private void UpdateBarValue()
    51.     {
    52.         staminaBar.value = currentStamina;
    53.     }
    54.  
    55.     private IEnumerator RegenStamina()
    56.     {
    57.         yield return new WaitForSeconds(2);
    58.         while(currentStamina < objStamina.staminaCurrent && IsDepleted == false)
    59.         {
    60.             //Not sure what you are trying to achieve.
    61.             //(objStamina.staminaCurrent / objStamina.staminaCurrent will always be 1.)
    62.             currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
    63.             UpdateBarValue();
    64.             yield return regenTick;
    65.         }
    66.         if (currentStamina <= 0)
    67.         {
    68.             Debug.Log("Stamina Depleated");
    69.             yield return new WaitForSeconds(2);
    70.             while (currentStamina < objStamina.staminaCurrent && IsDepleted)
    71.             {
    72.                 //Not sure what you are trying to achieve.
    73.                 //(objStamina.staminaCurrent / objStamina.staminaCurrent will always be 1.)
    74.                 currentStamina += objStamina.staminaCurrent / objStamina.staminaCurrent;
    75.                 UpdateBarValue();
    76.                 yield return regenTick;
    77.             }
    78.         }
    79.         regen = null;
    80.     }
    81.  
    82.     private void DepleteStaminaPassively()
    83.     {
    84.         if (currentStamina > 10)
    85.         {
    86.             staminaBar.value -= 1;
    87.             objStamina.staminaCurrent -= 1;
    88.         }
    89.     }
     
    BLACKHAWK4547 likes this.
  5. BLACKHAWK4547

    BLACKHAWK4547

    Joined:
    Mar 24, 2020
    Posts:
    6
    this actually helped a lot thankyou i didn't realize how messy my code was that made things so much easier thankyou again
     
    Franco_Voisard likes this.
  6. Franco_Voisard

    Franco_Voisard

    Joined:
    Apr 30, 2018
    Posts:
    20
    Just a little bit of refactor, remove duplication of your code it could actually help a lot while debugging. Please let me know if you find the bug :p
     
  7. BLACKHAWK4547

    BLACKHAWK4547

    Joined:
    Mar 24, 2020
    Posts:
    6
    i did find the problem restructuring it helped me see that i got myself confused by having objStamina.staminaCurrent and currentStamina as 2 diffrent ints or at least i think thats what i got wrong by adding currentSamina to the function passiveStaminaDepleation() it fixed the problem. probably wouldn't have copped the problem if i didnt restructure it despite reading over the code 25 times and a debug in every place i could put it haha thanyou for the help
     
    Franco_Voisard likes this.