Add a Max int value?
How do I make a variable 'max out' at a certain point? Another way to word this is, 'how do I make a level cap?
I think I'm supposed to place an If(Count == 50) statement, but The compiler says I can't because 'Count' is an int.
Thank you in advance!
Code:
void Update()
{
ItemInfo.text = itemName + "\nCost: " + cost + "\nPower: +" + clickpower;
_Slider.value = Click.Click / cost;
if (_Slider.value >= 1.0f)
{
GetComponent<Image>().color = Affordable;
}
else
{
GetComponent<Image>().color = standard;
}
}
Comment
what does the code snippet have to do with your question? What variable are you trying to limit?
Answer by Jessespike · Dec 11, 2015 at 10:21 PM
The easiest way to limit an integer is with Mathf.Clamp()
myInt = Mathf.Clamp(myInt, 0, 50);
You can use it inside a property:
private int someInt;
public int SomeInt {
get {return someInt;}
set {someInt = Mathf.Clamp(value, 0, 50);}
}
void Start () {
SomeInt = 4321;
Debug.Log(SomeInt); // outputs "50"
}
Answer by felixfors · Dec 13, 2015 at 03:30 PM
There is one more way to do this aswell. it requires some extra lines though but it makes it easier if you want something specific to happen at the set values aswell.
if(value >= 50)
{
value = 50;
}
if(value <=0)
{
value = 0;
}