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:
function Update () {
guiText.text = TheScore.ToString();
if (TheScore <= 0){
TheScore = 0;
}
}
Or do I use Mathf.Clamp? I tried this:
function Update () {
TheScore = (Mathf.clamp(0, 100)); // 0 = min 100 = max
}
and this
function Update () {
TheScore.int = Mathf.clamp(TheScore.int, 0, 100);
}
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.