Check if a game object is inside an array of GameObjects

I have an array called JunkTiles which contains all the prefabs of the junkTiles and i also have a mapTiles which contain the completed map. (Just to be clear the generatedMap contain instantiated GameObjects from prefabs)
Now when i spawn enemies I want to check the enemies won’t spawn on those tiles.
I tried to get the position the enemy want to spawn and check if the tile in this location is a junk tile by search the JunkTile array , but it doesn’t seem to work, here’s the code:

    Vector3 GetValidSpawnPoint()
    {
        Vector3 spawnPoint;
        RandomlyGeneratedBackground bg = GameObject.FindObjectOfType<RandomlyGeneratedBackground>();
        int x, y;
        x = (int)Random.Range(0, bg.MapSize.x);
        y = (int)Random.Range(0, bg.MapSize.y);

        while (containGameObject(bg.junkTiles, RandomlyGeneratedBackground.generatedMap[x, y])) // if there's a junk tile there, set a new random
        {
            x = (int)Random.Range(0, bg.MapSize.x);
            y = (int)Random.Range(0, bg.MapSize.y);
            Debug.Log("new random");
        }

        spawnPoint = new Vector3(x, y,0);
        return spawnPoint;
     
    }
    bool containGameObject(GameObject[] goArray, GameObject objectToCheck)
    {
        foreach(GameObject gObject in goArray)
        {
            if (gObject == objectToCheck)
                return true;
        }
        return false;
    }

By the way the debug message never shows up, no matter what.

Have you made sure the junkTile array is correctly filled ?

Alternatively, have you considered using a component to signify a tile it is junk ?

i.e. when a tile becomes junk you call:

tileGO.AddComponent<JunkTile>();

And when you wish to remove the junk:

Destroy(tileGO.GetComponent<JunkTile>())

And your valid spawn point code becomes:

while (RandomlyGeneratedBackground.generatedMap[x, y].GetComponent<JunkTile>())
{
....
}

Thus avoiding looking up that junkTile array potentially many times, I don’t know how big this gets but it seems like it could become an issue if you have many junk tiles.

1 Like

Your post gave me the idea of just using a tag, i’ve added a “Junk” tag to all the junk tiles and just use this code:

  Vector3 GetValidSpawnPoint()
    {
        Vector3 spawnPoint;
        RandomlyGeneratedBackground bg = GameObject.FindObjectOfType<RandomlyGeneratedBackground>();
        int x, y;
        x = (int)Random.Range(0, bg.MapSize.x);
        y = (int)Random.Range(0, bg.MapSize.y);

        while (RandomlyGeneratedBackground.generatedMap[x, y].tag == "Junk") // if there's a junk tile there, set a new random
        {
            x = (int)Random.Range(0, bg.MapSize.x);
            y = (int)Random.Range(0, bg.MapSize.y);
            Debug.Log("new random");
        }

        spawnPoint = new Vector3(x, y,0);
        return spawnPoint;
       
    }

Thanks:smile:

1 Like

Hehe yep tags work too if you’re not already using them :wink: