Search Unity

Stopping A Value From Being Less Than Zero

Discussion in 'Scripting' started by JStaples, Mar 24, 2013.

  1. JStaples

    JStaples

    Joined:
    Jan 21, 2013
    Posts:
    11
    PROBLEM SOLVED

    I had a brain blast shortly after writing this post, but it may be of use to someone someday. All i had to do was move the section of the script that reads:
    Code (csharp):
    1. if (energyCur< 0){
    2.    energyCur= 0;
    3.    }
    4.  
    down into when the shift key is pressed

    I also added in a feature to stop the bar from going below zero as well.
    Code (csharp):
    1. if (scale < 0){
    2. scale =0;
    3. }

    Here is the full code for reference:

    UPDATED CODE
    Code (csharp):
    1. var scale = 1.0f;
    2. public var energyStart: float;
    3. public var energyCur: float;
    4. public var energyMax= 100;
    5. public var energyCostPerSecond: float= 1;
    6.  
    7.    function changeEnergy(Change:float){
    8.    energyCur += Change;
    9.    if (energyCur> energyMax){
    10.    energyCur = 100;}
    11. }  
    12.  
    13. function OnGUI() {
    14.     GUI.backgroundColor = Color.yellow;
    15.     GUI.Button(Rect(10, 40, 100 * scale, 20), "Energy");
    16.    scale = energyCur / energyMax;
    17. if (Input.GetKey(KeyCode.LeftShift))
    18. {
    19.     energyCur -= energyCostPerSecond * Time.deltaTime;
    20. if (energyCur< 0){
    21.    energyCur= 0;
    22.    }
    23. if (scale < 0){
    24. scale =0;
    25. }
    26. }
    27. }
    28.  
    29.      function OnControllerColliderHit(hit : ControllerColliderHit) {
    30.         if (hit.gameObject.tag == "Player")
    31.             return;
    32.         Debug.Log("You hit " + hit.gameObject.tag);
    33.         if(hit.gameObject.tag == "ground")
    34.         {
    35.             //Destroy(hit.gameObject);
    36.             changeEnergy (1);
    37.         }
    /*



    Hello again,

    Im having a minor issue that is really bugging me. I have a javascript file for a regenerating bar, it works fine, and i managed to get it working, but when the bar reaches zero it continues depleting, going into negative values.

    the section of my script that has the code:
    Code (csharp):
    1. function changeEnergy(Change:float){
    2.    energyCur += Change;
    3.    if (energyCur> energyMax){
    4.    energyCur = 100;}
    5.    
    6. if (energyCur< 0){
    7.    energyCur= 0;
    8.    }
    9. }  
    and the full script for reference.
    Code (csharp):
    1. var scale = 1.0f;
    2. public var energyStart: float;
    3. public var energyCur: float;
    4. public var energyMax= 100;
    5. public var energyCostPerSecond: float= 1;
    6.  
    7.    function changeEnergy(Change:float){
    8.    energyCur += Change;
    9.    if (energyCur> energyMax){
    10.    energyCur = 100;}
    11.    
    12. if (energyCur< 0){
    13.    energyCur= 0;
    14.    }
    15. }  
    16.  
    17.  
    18. function OnGUI() {
    19.     GUI.backgroundColor = Color.yellow;
    20.     GUI.Button(Rect(10, 40, 100 * scale, 20), "Energy");
    21.      
    22.    scale = energyCur / energyMax;
    23. if (Input.GetKey(KeyCode.LeftShift))
    24. {
    25.     energyCur -= energyCostPerSecond * Time.deltaTime;
    26. }
    27. }
    28.     function OnControllerColliderHit(hit : ControllerColliderHit) {
    29.         if (hit.gameObject.tag == "Player")
    30.             return;
    31.         Debug.Log("You hit " + hit.gameObject.tag);
    32.         if(hit.gameObject.tag == "ground")
    33.         { ;
    34.             changeEnergy (1);
    35.         }
    36.     }

    */
     
    Last edited: Mar 24, 2013
  2. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Use Mathf.Clamp, like Mathf.Clamp(energyCur,0,energyMax);