Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Integers Not Multiplying

Discussion in 'Scripting' started by FleshKnot, Jul 19, 2020.

  1. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StaminaCalculator : MonoBehaviour
    6. {
    7.     PlayerCharacterStats playerCharacterStats;
    8.     private int Endurance;
    9.  
    10.  
    11.     //Timer\\
    12.     protected float Timer;
    13.     private int delayAmount = 1;
    14.  
    15.     //Stamina\\
    16.     public int enduranceMultiplierValue = 4;
    17.     public int enduranceMultiplier;
    18.     private int staminaBaseAmount = 10;
    19.     private int maxStamina;
    20.     private int staminaRegneration;
    21.     public bool staminaFull;
    22.     public int staminaPool;
    23.  
    24.     void Awake(){
    25.         playerCharacterStats = gameObject.GetComponent<PlayerCharacterStats>();      
    26.     }
    27.  
    28.     void start(){
    29.      
    30.     }
    31.  
    32.     void update(){
    33.         Timer += Time.deltaTime;
    34.         Endurance = playerCharacterStats.Endurance;
    35.  
    36.         //Stamina Management & Generation\\
    37.         enduranceMultiplier = (enduranceMultiplierValue * Endurance);
    38.         maxStamina = staminaBaseAmount + enduranceMultiplier;
    39.         staminaPool = maxStamina;    
    40.      
    41.         if (staminaPool == maxStamina){
    42.             staminaFull = true;          
    43.         }
    44.         while (staminaFull == false){
    45.             if(Timer >= delayAmount){
    46.                 Timer = 0;
    47.                 staminaPool++;
    48.             }
    49.  
    50.         }
    51.  
    52.     }
    53. }
    54.  
    The line that's not calculating properly is enduranceMultiplier = (enduranceMultiplierValue * Endurance);
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    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.
     
    FleshKnot likes this.
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,498
    "void update(){" I'm pretty sure needs to have a capital U. Also consider Timer -= delayAmount instead of Timer = 0
    oh yeah! if (!staminaFull) ..
     
    Bunny83 and FleshKnot like this.
  4. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    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.
     
    adamgolden likes this.
  5. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,498
    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.
     
    PraetorBlue, Bunny83 and mopthrow like this.