Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Check if a game object is inside an array of GameObjects

Discussion in 'Scripting' started by ShokeR0, Mar 7, 2016.

  1. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    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:

    Code (CSharp):
    1.     Vector3 GetValidSpawnPoint()
    2.     {
    3.         Vector3 spawnPoint;
    4.         RandomlyGeneratedBackground bg = GameObject.FindObjectOfType<RandomlyGeneratedBackground>();
    5.         int x, y;
    6.         x = (int)Random.Range(0, bg.MapSize.x);
    7.         y = (int)Random.Range(0, bg.MapSize.y);
    8.  
    9.         while (containGameObject(bg.junkTiles, RandomlyGeneratedBackground.generatedMap[x, y])) // if there's a junk tile there, set a new random
    10.         {
    11.             x = (int)Random.Range(0, bg.MapSize.x);
    12.             y = (int)Random.Range(0, bg.MapSize.y);
    13.             Debug.Log("new random");
    14.         }
    15.  
    16.         spawnPoint = new Vector3(x, y,0);
    17.         return spawnPoint;
    18.      
    19.     }
    20.     bool containGameObject(GameObject[] goArray, GameObject objectToCheck)
    21.     {
    22.         foreach(GameObject gObject in goArray)
    23.         {
    24.             if (gObject == objectToCheck)
    25.                 return true;
    26.         }
    27.         return false;
    28.     }
    By the way the debug message never shows up, no matter what.
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    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:
    Code (CSharp):
    1. tileGO.AddComponent<JunkTile>();
    And when you wish to remove the junk:
    Code (CSharp):
    1. Destroy(tileGO.GetComponent<JunkTile>())
    And your valid spawn point code becomes:
    Code (CSharp):
    1. while (RandomlyGeneratedBackground.generatedMap[x, y].GetComponent<JunkTile>())
    2. {
    3. ....
    4. }
    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.
     
    ShokeR0 likes this.
  3. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    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:

    Code (CSharp):
    1.   Vector3 GetValidSpawnPoint()
    2.     {
    3.         Vector3 spawnPoint;
    4.         RandomlyGeneratedBackground bg = GameObject.FindObjectOfType<RandomlyGeneratedBackground>();
    5.         int x, y;
    6.         x = (int)Random.Range(0, bg.MapSize.x);
    7.         y = (int)Random.Range(0, bg.MapSize.y);
    8.  
    9.         while (RandomlyGeneratedBackground.generatedMap[x, y].tag == "Junk") // if there's a junk tile there, set a new random
    10.         {
    11.             x = (int)Random.Range(0, bg.MapSize.x);
    12.             y = (int)Random.Range(0, bg.MapSize.y);
    13.             Debug.Log("new random");
    14.         }
    15.  
    16.         spawnPoint = new Vector3(x, y,0);
    17.         return spawnPoint;
    18.        
    19.     }
    Thanks:D
     
    _met44 likes this.
  4. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hehe yep tags work too if you're not already using them ;)