# Number Range

How do I create range of numbers(int) from min to max ?.

I want min to be 0 and max to be 100.

I want to check if number is in this range and prevent it from leaving that range.

Here’s the code:

``````    var Skin : GUISkin;
static var att = (Mathf.Clamp(50, 0, 100));
static var bck = (Mathf.Clamp(50, 0, 100));
static var rem = (Mathf.Clamp(25, 0, 100));
var btnTextureRight : Texture;
var btnTextureLeft : Texture;

function OnGUI () {

if (GUI.Button(Rect(100,200,25,25),btnTextureRight)){
att++;
rem--;
}
if (GUI.Button(Rect(10,200,25,25),btnTextureLeft)){
att--;
rem++;
}
if (GUI.Button(Rect(100,250,25,25),btnTextureRight)){
bck++;
rem--;
}
if (GUI.Button(Rect(10,250,25,25),btnTextureLeft)){
bck--;
rem++;
}
GUI.Label(Rect(60,200,90,50),att.ToString());
GUI.Label(Rect(60,250,90,50),bck.ToString());
GUI.Label(Rect(60,300,90,50),rem.ToString());
}
``````

But number is crossing my range.

Any idea how to fix this problem?

couldn’t you use an array to store multiple values in a specific range?

Have you tried Mathf.Clamp (might have to change to int) or possibly just use Random.Range and add 25 or whatever you want your starting number to be.
Though I’m not exactly sure whether that’s what you’re looking for.

It sounds like you are looking for Mathf.Clamp.

http://unity3d.com/support/documentation/ScriptReference/Mathf.Clamp.html (The second one is for ints)

``````att = Mathf.Clamp(att + 1, 0, 100);
rem = Mathf.Clamp(rem - 1, 0, 100);
``````