Integers Not Multiplying

Good morning.
For some reason, a set of integers I have are not multiplying at all. Regardless of whatever I do, the value in the inspector always remains 0, and the values that are lower in the hierarchy of the calculation remain 0 as well. It’s a very simple code and I don’t understand why it’s not working.

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

public class StaminaCalculator : MonoBehaviour
{
    PlayerCharacterStats playerCharacterStats;
    private int Endurance;


    //Timer\\
    protected float Timer;
    private int delayAmount = 1;

    //Stamina\\
    public int enduranceMultiplierValue = 4;
    public int enduranceMultiplier;
    private int staminaBaseAmount = 10;
    private int maxStamina;
    private int staminaRegneration;
    public bool staminaFull;
    public int staminaPool;

    void Awake(){
        playerCharacterStats = gameObject.GetComponent<PlayerCharacterStats>();      
    }

    void start(){
      
    }

    void update(){
        Timer += Time.deltaTime;
        Endurance = playerCharacterStats.Endurance;

        //Stamina Management & Generation\\
        enduranceMultiplier = (enduranceMultiplierValue * Endurance);
        maxStamina = staminaBaseAmount + enduranceMultiplier;
        staminaPool = maxStamina;     
      
        if (staminaPool == maxStamina){
            staminaFull = true;          
        }
        while (staminaFull == false){
            if(Timer >= delayAmount){
                Timer = 0;
                staminaPool++;
            }

        }

    }
}

The line that’s not calculating properly is enduranceMultiplier = (enduranceMultiplierValue * Endurance);

Change your while to an if. The update method is called by unity every frame and you have a while statement that has nothing contained which will end it so all it is going to do is prevent any further code from executing.

1 Like

“void update(){” I’m pretty sure needs to have a capital U. Also consider Timer -= delayAmount instead of Timer = 0

oh yeah! if (!staminaFull) …

2 Likes

Thank you both. I am trying to work on my attention to detail. The lower case U is for sure the issue, and I will implement the if statement instead of the while loop.Thank you both.

1 Like

The reason you’d want to use “Timer -= delayAmount” is precision. If you just set Timer = 0 after an interval, there will be either time left over that you’re losing every time (and the lower the framerate, the more inconsistent the gradual increase will become). However, by reducing the Timer value by the delayAmount instead, you ensure that the time left each time will carry over and be counted towards the next delay.

3 Likes