- Home /
If value is multiple of
Hey guys,
Quick programming / unity question.
I want to make an inventory with a 3x3 grid. The highlight starts at 0,0. If player presses right, the highlight moves to 1, 0 and now he's on the second slot of the inventory.
Now the thing is - if the player is on the third slot and he presses right, the highlight should not just to 4,0, but to 0, -1 (which is slot 4).
And the same thing should happen when the cursor is at 6, 9, 12, 15 - every time the cursor is at a multiple of 3.
Is there a clever way of doing this?
Answer by Mike 3 · Jun 10, 2011 at 01:26 PM
You can use the % operator to do it
if (val % 3 == 0)
{
//val is a multiple of 3, or 0
}
sometimes the simplicity of even such a small operator let's me stand in awe and marvel over the opportunities. No, seriously, math is fun and a friend of game devs!
Cheers $$anonymous$$ike, +rep for $$anonymous$$ching me something new today. :-)
Your answer
![](https://koobas.hobune.stream/wayback/20220612102531im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do use the % operator? 2 Answers
How to multiply two vector4 3 Answers
Is there a modulus operator in Unity JavaScript? 4 Answers
Need help with Inventory stacker. PLEASE. Almost working! 0 Answers
unity3d simple inventory 1 Answer