Is there an operand for saying “Cannot equal”:
currentscore -= 10;
appleslider.value = currentscore;
Basically I never want current score to be less than 0, it can be equal to 0.
How would I do this?
Is there an operand for saying “Cannot equal”:
currentscore -= 10;
appleslider.value = currentscore;
Basically I never want current score to be less than 0, it can be equal to 0.
How would I do this?
So I tried using Mathf.Clamp and well it’s not working ![]()
Does that mean If my slider has a value between 0 - 20 would this be correct?
currentscore += 10;
appleslider.value = currentscore;
Debug.Log(Mathf.Clamp(currentscore, 0, 20));
I tested it and it still outputs -10, any reason why? ![]()
When you’ve done whatever you’re going to do to the score the last thing you should do is Clamp it. All it does is see if the value is outside the given range and if it is then make it either the min/max given value. As long as this is the last thing you do to the score, it will always be locked in that range.
You have to assign the value to the variable. Clamp does not operate on a variable “in place”.
–Eric
Your code should look like this:
currentscore -= 10;
currentscore=Mathf.Clamp(currentscore,0,20);
appleslider.value = currentscore;
Awesome! thanks guys!
Here is my solution:
public void AddPoints()
{
currentscore += 1;
currentscore = (Mathf.Clamp(currentscore, 0, 20));
Debug.Log(currentscore);
appleslider.value = currentscore;
}
public void RemovePoints()
{
currentscore -= 1;
currentscore = (Mathf.Clamp(currentscore, 0, 20));
Debug.Log(currentscore);
appleslider.value = currentscore;
}
EDIT: Just seen your post ElDo as I posted! ![]()
It would be better just to do
currentscore = Mathf.Clamp (currentscore-1, 0, 20);
Also probably better just to have one function:
void ChangeScore (int amount) {
currentscore = Mathf.Clamp (currentscore + amount, 0, 20);
appleslider.value = currentscore;
}
Then you can do e.g. “ChangeScore (-1);”.
–Eric