- Home /
how do i make a loop for a boolean?
hi guys, how do i make a loop for a boolean and compare it to an array.length?
var isCorrect : boolean;
function finish() {
if (dragTgtScript.isCorrect == true){
for(var j = 0; j <= dragObjIndex.length; j ++ ) {
if (dragTgtScript.isCorrect[j] == dragObjIndex.length){
//winGame.animation.Play("move");
print("win game!");
}
}
}
}
Answer by tomka · May 30, 2012 at 07:10 AM
I think you mean, "How do I have multiple guard conditions".
You can do something like this:
var isCorrect : boolean = false;
function finish() {
for (var j = 0; j < 10 && !isCorrect; ++j) {
//do some stuff
}
}
or, if you want to break out of a loop, you can include "break"
e.g.
for (int i = 0; i < 10; ++i) {
if (i == 3) break;
else Debug.Log("" + i);
}
Will produce the following output:
0 1 2
Note: only up to "2" because once "i == 3" is true, we "break" the loop.
Answer by connie · May 30, 2012 at 08:35 AM
i do a drag and drop objects. and when the objects snap: isCorrect = true. and the dragObjIndex is array. so i wanna make if all objects snapped to its position. you win the game. this is the snap
function snap (objToSnap: GameObject, posToSnap: Vector3) {
objToSnap.transform.position.x = posToSnap.x;
objToSnap.transform.position.y = posToSnap.y;
canSnap = true;
isCorrect = true;
dragObjScript.isMatch = true;
//objToSnap.name = "Snap";
print("Snap");
}
Your answer
Follow this Question
Related Questions
variables based on array.length not updating in JS 1 Answer
How to correctly shorten this script using arrays and iterations 2 Answers
Loop through array until certain value is found. 2 Answers
Not all code paths return a value in boolean loop 2 Answers
Adding an object to an array only if it is not in the array 2 Answers