- Home /
How to check if variable is equal to range of integers
I'm trying to make a scoring system for my game where backgrounds will change upon meeting a certain condition. So I have a variable int "SCORE", every time my player game object collides with a variety of loots inside my scene, SCORE is incremented by 1 or by 4 points. My condition to change background is if the SCORE is equal to 11 or up to 14. Is there much easier way to write that logic?
this is what I have tried so far:
void checkpoint() { if (SCORE == 11 || SCORE == 12 || SCORE == 13 || SCORE == 14 ) { changeBG(); } if (SCORE == 20 || SCORE == 21 || SCORE == 23 || SCORE == 24) { changeBG(); } }
but every time the SCORE hits those integers inside the condition the changeBG method is called again.
Answer by Shen-Kit · Apr 24, 2020 at 12:56 PM
You should use greater than or equal to >=
and less than or equal to <=
if (SCORE >= 11 && SCORE <= 14 )
{
changeBG();
}
else if (SCORE >= 20 && SCORE <= 24)
{
changeBG();
}
If this works please mark the question answered. If it doesn't please ask any questions I'm happy to help :)
Answer by Bunny83 · Apr 24, 2020 at 01:22 PM
Your numbers / ranges looks a bit weird. Are you sure you want to start the second range at 20 and leave out 22?
If you just want to change the background whenever the level number ends in 1, 2, 3 or 4 you can simply do:
int tmp = SCORE % 10;
if (tmp >= 1 && tmp <= 4)
{
changeBG();
}
This would call changeBG at a score of 1,2,3,4,11,12,13,14,21,22,23,24,31,32,33,34,41,42, .....
of course if you want to start this pattern at 11 and not at 1 you can add an additional condition
if (SCORE >= 11 && tmp >= 1 && tmp <= 4)
{
changeBG();
}
Your answer
Follow this Question
Related Questions
Need Help with Dual Scoring System C# (score over time and kill score) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to set my score to 0 when hit restart ; 1 Answer
3 star time base system 0 Answers