Search Unity

How can I add randomly instantiated gameobjects to a list?

Discussion in 'Scripting' started by jleven22, Jan 4, 2020.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    I'm trying to generate randomly selected enemies in a room.

    The catch is I need the instantiated enemy gameobjects to be added to my Enemies list. This is because I need to count how many enemies are left to open the doors of the room.

    Here's what I've got for the enemy counter:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RoomCenter : MonoBehaviour
    6. {
    7.     public bool openWhenEnemiesCleared;
    8.  
    9.     public List<GameObject> enemies = new List<GameObject>();
    10.  
    11.     public Room theRoom;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         if (openWhenEnemiesCleared)
    17.         {
    18.             theRoom.closeWhenEntered = true;
    19.         }
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (enemies.Count > 0 && theRoom.roomActive && openWhenEnemiesCleared)
    26.         {
    27.             for (int i = 0; i < enemies.Count; i++)
    28.             {
    29.                 if (enemies[i] == null)
    30.                 {
    31.                     enemies.RemoveAt(i);
    32.                     i--;
    33.                 }
    34.             }
    35.  
    36.             if (enemies.Count == 0)
    37.             {
    38.                 theRoom.OpenDoors();
    39.             }
    40.  
    41.         }
    42.     }
    43. }
    44.  
    And this is what I'm looking to create for my random enemy spawner:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MonstersSpawnerControl : MonoBehaviour {
    6.  
    7.     public Transform[] spawnPoints;
    8.     public GameObject[] monsters;
    9.     int randomSpawnPoint, randomMonster;
    10.     public static bool spawnAllowed;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         spawnAllowed = true;
    15.         InvokeRepeating ("SpawnAMonster", 0f, 1f);
    16.     }
    17.  
    18.     void SpawnAMonster()
    19.     {
    20.         if (spawnAllowed) {
    21.             randomSpawnPoint = Random.Range (0, spawnPoints.Length);
    22.             randomMonster = Random.Range (0, monsters.Length);
    23.             Instantiate (monsters [randomMonster], spawnPoints [randomSpawnPoint].position,
    24.                 Quaternion.identity);
    25.         }
    26.     }
    27.  
    28. }
    29.  
    So my question is how to add the instantiated gameobjects from the "monsters" array into the "enemies" list.


    TLDR - I want to add instantiated enemy gameobjects to my Enemies list.
     
  2. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    762
    In RoomCenter.cs

    Code (csharp):
    1. public void AddEnemyToList(GameObject newEnemy)
    2. {
    3. enemies.Add(newEnemy);
    4. }
    In MonstersSpawnerControl, change line 23 to

    Code (csharp):
    1. GameObject spawnedEnemy = Instantiate(monsters[randomMonster], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
    2. roomCenter.AddEnemyToList(spawnedEnemy);
    You'll need to declare a new variable at the top to cache the reference to RoomCenter.

    Code (csharp):
    1. private RoomCenter roomCenter;
    2.  
    3. private void Start()
    4. {
    5. roomCenter = FindObjectOfType<RoomCenter>();
    6. }
     
    jleven22 likes this.
  3. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Ok, YOU are a genius!

    One issue I'm encountering -- it seems the enemies are instantiating at spawn points every second. I'm a little new to InvokeRepeating, but I'm wondering if it's the right thing. I only want one enemy to spawn once, and that's it.

    Any tips?
     
  4. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Actually it seems I have figured it out.

    I just used SpawnEnemy() instead of InvokeRepeating.

    I also changed the private RoomCenter roomcenter to public, and just made sure I connected in the inspector. Looks like it's working for now! I'll check back if I encounter another issue.

    Thank you!
     
  5. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    762
    If you're doing it that way, make sure to remove the line from Start(), otherwise it will overwrite what you assign.
     
  6. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    Done, thanks!