- Home /
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?
Can you elaborate on what you are trying to do? Are you trying to create a function that goes from 25 to whatever, or just check to make sure that a number falls in a certain range?
You took the time to write a question and then didn't bother to make sure it was clear.
Give this a read: http://answers.unity3d.com/questions/133869/how-to-ask-a-good-question.html
dibonaj, I want to check if number falls in a certain range (from 0 to 100).
This is very confusing...
Do you want to create a random number, like you say in your question, or check a value, like you say in your comment?
Very unclear.
I said in question that I don't want Random.Range. I want to check if number is in range from 0 to 100, and prevent it from leaving that range
Answer by flaviusxvii · Jul 07, 2011 at 04:41 PM
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);
Can you please check my code and write me where did I make the mistake?
I edited my answer with an example of what you need to do..
Answer by deeredman1991 · Jul 07, 2011 at 04:22 PM
couldn't you use an array to store multiple values in a specific range?
Answer by Sebas · Jul 07, 2011 at 04:29 PM
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.
Your answer

Follow this Question
Related Questions
Mathf.Clamp an int value 4 Answers
Does anyone know why this is wrong? 2 Answers
Making a plain number range 2 Answers
Script so that, my enemies only see my player at a certain range. 4 Answers
Damage Within Range 2 Answers