Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make an Integer not go below 0

Discussion in 'Scripting' started by Rush-Rage-Games, Aug 28, 2012.

  1. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Hello Guys!

    I was wondering if there was an easy way to make an integer not go below zero and into negative numbers! I searched the docs and found Mathf.CeilToInt, but it is not making an sense to me. And I'm guessing I would not do something like this:

    Code (csharp):
    1. function Update () {
    2.     guiText.text = TheScore.ToString();
    3.    
    4.     if (TheScore <= 0){
    5.         TheScore = 0;
    6.     }
    7. }
    Or do I use Mathf.Clamp? I tried this:

    Code (csharp):
    1. function Update () {
    2.     TheScore = (Mathf.clamp(0, 100));  // 0 = min 100 = max
    3. }
    and this
    Code (csharp):
    1. function Update () {
    2. TheScore.int = Mathf.clamp(TheScore.int, 0, 100);
    3. }
    But it gives me this error:

    Thank you so much for your help!


    --Rush Rage
     
    Last edited: Aug 28, 2012
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    TheScore = Mathf.clamp(TheScore, 0, 100);
     
    ghellee likes this.
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1.  
    2. Mathf.Clamp
    3. not
    4. Mathf.clamp
    5.  
    Also, if you don't have a maximum limit it would be:
    Code (csharp):
    1.  
    2. Mathf.Clamp (0, Mathf.Infinity);
    3.  
     
    ghellee likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There is no Mathf.clamp; you mean Mathf.Clamp.

    Aside from putting the guiText.text line at the end (in case the score is below 0), yes that is in fact what you'd do. (Although "< 0" would be slightly more efficient, since you wouldn't continuously set the score to 0 if it's already 0.)

    However, it would be significantly more efficient not to have this in Update, since it's highly unlikely the score changes every frame. Instead, just make a function that's only called when the score changes, and put the above code in that function.

    --Eric
     
  5. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Hmm, still gives me this error when I click play:

     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're still trying to use Mathf.clamp, when there is no such thing. Anyway it's better just to use the "if score < 0, score = 0" technique.

    --Eric
     
  7. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Wow! 3 responses with in 5 minutes! This community is awesome.

    Thanks for your guys posts, I figured it out!

    --Rush Rage


    Edit:


    When I checked this post I only saw his post not yours. But when I refreshed it I saw yours and Daniels.
     
    Last edited: Aug 28, 2012
    SamSpain likes this.