- Home /
Logic error help: arrays and nested for loops
I'm using this to make my character select in a tower defense game. You pick 5 towers and then I'm reading the static boolean values from another script (this script), in another scene (my gameplay scene).
this is my script
void Start(){
for (int i = 0; i < 5; i++) {
Vector2 buttonPos = new Vector2 (i + 1, 6);
for (int p = 0; p < numberOfAvailableDefenders; p++) {
if (selectedBoolArray [p] == true) {
GameObject buttonSpawn = Instantiate (buttonArray [p], buttonPos, Quaternion.identity) as GameObject;
buttonSpawn.transform.parent = transform;
selectedBoolArray [p] = false;
//setTrueAfter [p] = selectedBoolArray [p];
break;
}
}
}
}
like I said the error is a logic one. Everything is working fine and all of the Boolean values are coming through correctly. The issue is that the selected defenders aren't the ones in the game scene. The Defenders you get are just random.
Answer by allenallenallen · Dec 29, 2016 at 02:34 AM
I think I see your problem:
for (int p = 0; p < numberOfAvailableDefenders; p++) {
if (selectedBoolArray [p] == true) {
}
}
The p is supposed to keep track of how many defenders the player has selected, right? Yet you are using it to check the existing boolean values. This means you will ALWAYS ONLY check the boolean values from 0 to 4. The rest of the defenders are then completely ignored.
To be honest, I don't see why you need to make the limit of 5 defenders here. It would be easier if you do it while the player is selecting.