When my player eats a blue apple he gives him a point but when he eats a red apple he decreases a point, if I eat I have 2 points and I eat 3 red apples it will be -1, I want to know if they can pass me a code so that do not drop below 0
Check out the Mathf.Clamp documentation. This method will clamp the result of your equation to a minimum or maximum value so that if you had
float score = 0;
float blueApple = 1;
float redApple = 1;
private void Start()
{
score = Mathf.Clamp((blueApple * 2) - (redApple * 3), 0, Mathf.Infinity); //two blue apples and 3 red apples to recreate the example scenario
Debug.Log($"Score value clamped to {score}");
}
Your value would be clamped to 0 instead of -1.
Can’t you just use a simple if statement? For example, if your score was stored in a variable called score
, before you decreased the number of apples, check if the number of apples is greater than 0:
if (score > 0)
{
score--;
}