So i’m making this grid based game and below is the method that is supposed to handle combat. First i loop through the players object and then through the enemies objects. If there’s a square object in the oponents battlefield it is supposed to destroy it (and destroy itself), but not more than just one! This is where it goes wrong. Every time i play the game the triangle kills all the square objects on the opponents battlefield. What am i doing wrong?
void handleCombat(){
GameObject [,] enemybattleFieldObjects = enemyBehaviourScript.enemyBattleFieldObjects;
for(int i = 0;i < width;i++){
// triangle behaviour
if (battleFieldObjects[i, 0] != null)
{
if (battleFieldObjects[i, 0].tag == "triangle")
{
bool triangleDestroyed = false;
for (int j = 0; j < width; j++)
{
Debug.Log("i = " + i);
if (enemybattleFieldObjects[j, 0].tag == "square")
{
Debug.Log("enemybattlefieldofject 1 = " + enemybattleFieldObjects[0,0]);
Debug.Log("enemybattlefieldofject 2 = " + enemybattleFieldObjects[1, 0]);
Debug.Log("enemybattlefieldofject 3 = " + enemybattleFieldObjects[2, 0]);
Destroy(battleFieldObjects[i, 0]);
Destroy(enemybattleFieldObjects[j, 0]);
battleFieldObjects[i, 0] = null;
enemybattleFieldObjects[j, 0] = null;
triangleDestroyed = true;
break;
}
else if (enemybattleFieldObjects[j, 0].tag == "circle")
{
Destroy(enemybattleFieldObjects[j, 0]);
enemybattleFieldObjects[j, 0] = null;
break;
}
}
if (triangleDestroyed == false)
{
playerstatistics.decreaseP2Health(3);
}
}
//square combat
else if (battleFieldObjects[i, 0].tag == "square")
{
for (int j = 0; j < width; j++)
{
if (enemybattleFieldObjects[j, 0].tag == "triangle")
{
Destroy(battleFieldObjects[i, 0]);
Destroy(enemybattleFieldObjects[j, 0]);
battleFieldObjects[i, 0] = null;
enemybattleFieldObjects[j, 0] = null;
}
}
}
//circle combat
else if (battleFieldObjects[i, 0].tag == "circle")
{
bool circleDestroyed = false;
for (int j = 0; j < width; j++)
{
if (enemybattleFieldObjects[j, 0].tag == "triangle")
{
Destroy(battleFieldObjects[i, 0]);
battleFieldObjects[i, 0] = null;
circleDestroyed = true;
}
}
if (circleDestroyed == false)
{
playerstatistics.increaseGoldGain(2);
}
}
}
}
}