- Home /
crazy loop !!! HELP ME
hi
i have a loop :
for(i=32;i>=0;i--)
{
if(i%8 == 1) { StartPositionX = -40; }
if(i<8)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
else if(i>=8 && i<16)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
else if(i>=16 && i<24)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
else if(i>=24)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
}
this loop print "StartPositionX" variable for me after minus it by 4 and reset it to "-40" after i%8 equals 1 .
but when last condition comes , result goes crazy !!!! here is the result(in image) :
i don't know why "StartPositionX" variable in last condition don't minus by 4 !!!!!
How can I Fix It ?
they all do $$anonymous$$us by 4, except as i = 1,9,17,25... aka when i%8 = 1
what is your exact expected behaviour?
Your starting value of StartPositionX is -41.5f.
And why do have all these if statements to do the exact same thing? all you've got is, for all values of i, StartPositionX -= 4;
I've a feeling you have misunderstood the meaning of this line:
if(i%8 == 1) { StartPositionX = -40; }
What, in words, do you want that to do?
@Qasem2014 - Your script still has lots of issues as tanoshimi and Scribe have commented. For example your current code:
if(i%8 == 1) { StartPositionX = -40; }
if(i<8)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
else if(i>=8 && i<16)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
else if(i>=16 && i<24)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
else if(i>=24)
{ StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
The lines above are currently the same as writing (i.e. could be replaced by) these next two lines alone
if(i%8 == 1) { StartPositionX = -40; }
StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX);
Is this by design?
richyrich , its a sample of my code . and i should set "StartPositionX" variable private or change it to -40 in component window .
i have a -41 number for it before i change it to -40 . because of that my first result is different from what i expect .
Answer by KillMobil · Jan 08, 2015 at 12:50 PM
I just ran the script and look like it was fine on my side
is it possible that the Variable StartPositionX gets affected by other factors? Is it a public variable? is set on to be equal transform later in your script?
i make "StartPositionX" variable private and problem solved :)
before i change my variable to -40 , i have -44 already assign to it in component window
Your answer
Follow this Question
Related Questions
"for" loop - DrawTexture problem. 0 Answers
For loop C#? 2 Answers
Using for as while. 3 Answers
Shorten code with FOR loop 1 Answer