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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Energy bar doesnt update when i apply my cooldown

Discussion in 'Scripting' started by boooels, Nov 15, 2018.

  1. boooels

    boooels

    Joined:
    Oct 26, 2018
    Posts:
    31
    So i have an issue, where my energy script works just fine, i subtract the energy cost from my energy bar and the bar regens energy. My issue is, when i apply a cooldown effect to this energybar. Either it doesnt any energi or it doesnt update the energybar.
    Heres my code for it:
    Code (CSharp):
    1.  public void EnergyCost(float aCostValue)
    2.     {
    3.         if (currentEnergy >= abilityCost && Time.time > basisCooldown.nextFire)
    4.         {
    5.             basisCooldown.nextFire = Time.time + basisCooldown.cooldown; //Using basis cooldown script to check if the ability is ready, so we can use more energy
    6.  
    7.             //Deduct the energy spent from the current energy
    8.             currentEnergy -= aCostValue;
    9.             energyBar.value = CurrentEnergy();
    10.         }
    11.         //If there is insufficient energy alert and stop use
    12.         else
    13.         {
    14.             StartCoroutine(NotEnoughEnergy());
    15.         }
     
  2. ZombieTFK

    ZombieTFK

    Joined:
    Sep 6, 2016
    Posts:
    55
    If I think I understand you - Where do you cast your ability? Could you please show us your use of this function in context.
     
  3. boooels

    boooels

    Joined:
    Oct 26, 2018
    Posts:
    31
    Yeah, Sure i can show you a picture of the game and the entire script

    the game:
    bf4951f2383be63b08387a66fdbbdb09.png

    The energybar script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ResourceBar : MonoBehaviour {
    7.  
    8.     public float currentEnergy { get; set; }
    9.     public float maxEnergy { get; set; }
    10.  
    11.     public float abilityCost;
    12.     //These trigger buttons will be set in the ability scripts
    13.     public string p1TriggerButton1;
    14.     public string p1TriggerButton2;
    15.  
    16.     private bool stopECost = true;
    17.  
    18.     private BasisCooldown basisCooldown;
    19.  
    20.     private Text insufficientSP;
    21.     private Text SPAmount;
    22.  
    23.     [SerializeField]
    24.     private Slider energyBar;
    25.  
    26.     // Use this for initialization
    27.     void Start () {
    28.         //Load this gameobjects basis cooldown script so we can use them to check if we can use more energy
    29.         basisCooldown = gameObject.GetComponent<BasisCooldown>();
    30.  
    31.         maxEnergy = 10f;
    32.         //Resets to value on game load
    33.         currentEnergy = 7f;
    34.  
    35.         StartCoroutine(AddEnergy());
    36.     }
    37.    
    38.     // Update is called once per frame
    39.     void Update () {
    40.         energyBar = GameObject.Find("P1ResourceBar").GetComponent<Slider>();
    41.         energyBar.value = CurrentEnergy();
    42.  
    43.         SPAmount = GameObject.Find("SPAmount").GetComponent<Text>();
    44.         SPAmount.text = CurrentEnergy() * 10 + "SP";
    45.      
    46.         PlayerTriggers();
    47.     }
    48.  
    49.     public void EnergyCost(float aCostValue)
    50.     {
    51.         if (currentEnergy >= abilityCost && Time.time > basisCooldown.nextFire)
    52.         {
    53.             basisCooldown.nextFire = Time.time + basisCooldown.cooldown; //Using basis cooldown script to check if the ability is ready, so we can use more energy
    54.  
    55.             //Deduct the energy spent from the current energy
    56.             currentEnergy -= aCostValue;
    57.             energyBar.value = CurrentEnergy();
    58.         }
    59.         //If there is insufficient energy alert and stop use
    60.         else
    61.         {
    62.             StartCoroutine(NotEnoughEnergy());
    63.         }
    64.  
    65.         //If the current energy is less or 0 set it to 0 so we wont get negative energy
    66.         if (currentEnergy < 0f)
    67.         {
    68.             currentEnergy = 0f;
    69.         }
    70.     }
    71.  
    72.     public void PlayerTriggers()
    73.     {
    74.         //If player presses one of their ability buttons we subtract the abilities cost thorugh the EnergyCost() script
    75.         if (Input.GetButtonDown(p1TriggerButton1))
    76.         {
    77.             EnergyCost(abilityCost);
    78.         }
    79.     }
    80.  
    81.     float CurrentEnergy()
    82.     {
    83.         return currentEnergy / maxEnergy;
    84.     }
    85.  
    86.     public IEnumerator NotEnoughEnergy()
    87.     {
    88.         insufficientSP = GameObject.Find("InsufficientSP").GetComponent<Text>();
    89.         insufficientSP.text = "Not enough Energy...";
    90.  
    91.         yield return new WaitForSeconds(2);
    92.  
    93.         insufficientSP.text = "";
    94.     }
    95.  
    96.     public IEnumerator AddEnergy()
    97.     {
    98.         while (true)// loops forever
    99.         {
    100.             //if current energy is less than maxenergy
    101.             if(currentEnergy < maxEnergy)
    102.             {
    103.                 currentEnergy += 1; //increase energy with 1
    104.                 yield return new WaitForSeconds(3);
    105.             }
    106.  
    107.             else //if currentenergy is 10>=
    108.             {
    109.                 yield return null;
    110.             }
    111.         }
    112.     }
    113. }
    114.  
    Sorry if its a lot of code
     
    ZombieTFK likes this.
  4. ZombieTFK

    ZombieTFK

    Joined:
    Sep 6, 2016
    Posts:
    55
    [link] I copy and pasted your code, added the appropriate game objects to my scene and only modified what i needed to get the script running + log out when it was on cooldown, and things worked as expected. Hmm, check that your values in the inspector are correct? Or if you mutate the state (properties) of this or your BasisCooldown anywhere else in your codebase.

    Here is my modified version if you're interested
     
    boooels likes this.
  5. boooels

    boooels

    Joined:
    Oct 26, 2018
    Posts:
    31
    I put it in start, but my prefabs arent loaded at start. Thank you so much for your time!