I want to spawn a game object every time a public variable is 5 more than the last for example 5,10,15,20… but how can I do this? I was thinking of making something like
public var i:int;
if(i==5||i==10||i==15...){
//do something
}
but the problem is that this is suppose to go to infinity so how can I?maybe a list? or and array?
Think of your problems in terms of the sequence itself. When do you want the if
statement to execute? You want it to execute when i
is a multiple of 5
.
If you do a quick google search you will find that finding out whether a variable is a multiple of some number is quite easily done using the modulus
operator.
The modulus operator returns the remainder of the left hand side, when divided by the right hand side. In almost every language, the modulus operator is %
. So 2 % 10 = 2
and 20 % 8 = 4
.
Back to the problem, what is the remainder when something is exactly divisible by a number?
0
So your test is really easy to do:
i % 5 == 0