Searching exclusively for specific indexes in a array

I’m having troubles finalizing my battle system.
Currently its a direct single target, you make your move, they do the same, but that was mostly for testing of other components.

In the end, i would like there to be a 3x3 grid stored in a array. Where enemy’s spawn at random on the board. This I can manage.


So just for explanatory reasons lets say bottom left is box 0, middle left is box 3, top left 6 and finishing with the 8th square top right.


Let’s say I have a spell that should target in a X formation. This would affect grid locations 0,2,4,6,8.
So the first course of action would be verifying if there is a gameobject(enemy) present in only those specific locations.


/* I understand checking through the entire grid array if any of locations are taken. But not how to call upon a specific list of indexes in that array. */

And if so, call the damage method on to those gameobjects. Which is already prepared.


But I can’t seem to figure out the targeting system I would like to achieve and then checking if it hits anything.

Any kind of assistance or guidance would be greatly appreciated!

You should probably use a dictionary with keys of type int - for the grid square index - and values of type game object - for the object at that square. Then, you can create an array of indexes you wish to search through, and check if a game object is at that position. For example:

// ===== Dictionary Initialization =====

var gridDict = new Dictionary<int, GameObject>();

for (int i = 0; i < 9; i++)
{
     gridDict.Add(i, null);
}

// ===== Enemy Spawning =====

int randomEnemyIndex = Random.Range(0, 9);
var enemyObj = Instantiate(Enemy, enemySpawnPos, Quaternion.identity);

// Places enemy GO at a random grid position
gridDict[randomEnemyIndex] = enemyObj;

// ===== Cross Attack =====

var crossAttack = new int[] {0, 2, 4, 6, 8};

foreach (var index in crossAttack)
{
     // There's an enemy at one of the cross attack positions
     if (gridDict[index] != null)
     {
          // Destroys enemy
          Destroy(gridDict[index]);
          break;
     }
}

Hope that helps @BenoitMenard