1

I'm making a game in unity, and I have this 'if statement' that by every 5 waves my shop menu will become visible. The code does work, but I am certain I'm doing something wrong or could do something better!

if (waveCount == 5 || waveCount == 10 || waveCount == 15 || waveCount == 20 || waveCount == 25 || waveCount == 30 || waveCount == 35 || waveCount == 40 || waveCount == 45 || waveCount == 50)
{
    // yield return new WaitForSeconds(shopWait);
    shopPanel.SetActive(true);
}

As you can see the 'if statement' not that good, normally it continues all the way to waveCount == 100 but i cut that out. There must be a simpler or cleaner way to do this :/ but i just can't wrap my head around it :(

Edit 1:

Thanks, I didn't know much about modulo, know I know what I have to read about :)

2

9 Answers 9

7

You can use modulo operation:

if (waveCount % 5 == 0)

2

Yes, there are indeed simpler ways of doing this. If you use a little bit of maths and logic, you can figure this out.

Since you want to check whether the value of waveCount is a multiple of 5, you can use % to get the reminder of waveCount / 5. If that reminder is 0, waveCount is a multiple of 5.

if (waveCount % 5 == 0 && waveCount <= 100)

I added waveCount <= 100 to replicate your code's behaviour when waveCount is larger than 100 i.e. not get into the if statement.

Alternatively, you can put all the values into a list:

var list = new List<int>();
for (int i = 1 ; i <= 20 ; i++) {
    list.Add(i * 5);
}

And then check whether the list contains the number:

if (list.Contains(waveNumber))

The advantage of this is that if you decided to change how the game works and say that the shop menu can be opened at waves 9, 52, and 77, you just add the numbers to the list, without modifying the if statement. This provides a lot of flexibility.

2
if (waveCount % 5 == 0 && waveCount <= 50) {
    //...code
}
1

If your “if” statement's body just contains shopPanel.SetActive(true); you can do that without even using “if” like that.

shopPanel.SetActive(waveCount % 5 == 0 && waveCount <= 50);
0

Give it a try

if (waveCount % 5 == 0 && waveCount <= 50)
0

Use the modulo-operator:

if(waveCount % 5 == 0 && waveCount <= 100) ...

The operator calculates the remainder of an integer-divison. In your case the statement should return zero indicating that your number divided by 5 has no remainder.

0

Just to generalize: in case the data you have doesn't match a pattern, you can put all the things to match against in a set, then test the set for membership:

var thingsToMatch = Set(2, 5, 8, 14, 23, 80, 274...);
if (someNumber in thingsToMatch) {...} 

As long as you know the set isn't being recreated everytime the function is called, this has proven to be fairly fast. If your language doesn't automatically cache the set, you can make it a static variable of the function.

0

You can use the remainder operator for this:

if (waveCount % 5 == 0 && waveCount > 0 && waveCount <= 50)
{
    //yield return new WaitForSeconds(shopWait);
    shopPanel.SetActive(true);
}
0

You can test whether the remainder of the division by 5 is 0, which means that the number is divisible by 5.

if (waveCount % 5 == 0 && waveCount >= 5 && waveCount <= 50)

C# performs integer math on integer number types (int, long, uint, ...). Example:

13 / 5 = 2

I.e. you never get a decimal fraction part. The complementary operation is the modulo operation. It gives you the remainder of this division:

13 % 5 = 3

I.e., 13 / 5 is 2 plus remainder 3. Together, division and modulo operation allow you to perform the reverse operation.

(5 * (13 / 5)) + (13 % 5) =  
(5 *     2   ) + (   3  ) =  13

If you have irregular figures, quite different approaches are to use a switch statement:

switch (waveCount) {
    case 5:
    case 10:
    case 15:
    case 20:
    case 25:
    case 30:
    case 35:
    case 40:
    case 45:
    case 50:
        shopPanel.SetActive(true);
        break;
}

or an array of allowed values:

private static readonly int[] AllowedValues =
    new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };

if(Array.IndexOf(AllowedValues, waveCount) >= 0) { ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.