How can I add randomly instantiated gameobjects to a list?

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomCenter : MonoBehaviour
{
    public bool openWhenEnemiesCleared;

    public List<GameObject> enemies = new List<GameObject>();

    public Room theRoom;

    // Start is called before the first frame update
    void Start()
    {
        if (openWhenEnemiesCleared)
        {
            theRoom.closeWhenEntered = true;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (enemies.Count > 0 && theRoom.roomActive && openWhenEnemiesCleared)
        {
            for (int i = 0; i < enemies.Count; i++)
            {
                if (enemies[i] == null)
                {
                    enemies.RemoveAt(i);
                    i--;
                }
            }

            if (enemies.Count == 0)
            {
                theRoom.OpenDoors();
            }

        }
    }
}

And this is what I’m looking to create for my random enemy spawner:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonstersSpawnerControl : MonoBehaviour {

    public Transform[] spawnPoints;
    public GameObject[] monsters;
    int randomSpawnPoint, randomMonster;
    public static bool spawnAllowed;

    // Use this for initialization
    void Start () {
        spawnAllowed = true;
        InvokeRepeating ("SpawnAMonster", 0f, 1f);
    }

    void SpawnAMonster()
    {
        if (spawnAllowed) {
            randomSpawnPoint = Random.Range (0, spawnPoints.Length);
            randomMonster = Random.Range (0, monsters.Length);
            Instantiate (monsters [randomMonster], spawnPoints [randomSpawnPoint].position,
                Quaternion.identity);
        }
    }

}

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.

In RoomCenter.cs

public void AddEnemyToList(GameObject newEnemy) 
{
enemies.Add(newEnemy);
}

In MonstersSpawnerControl, change line 23 to

GameObject spawnedEnemy = Instantiate(monsters[randomMonster], spawnPoints[randomSpawnPoint].position, Quaternion.identity);
roomCenter.AddEnemyToList(spawnedEnemy);

You’ll need to declare a new variable at the top to cache the reference to RoomCenter.

private RoomCenter roomCenter;

private void Start()
{
roomCenter = FindObjectOfType<RoomCenter>();
}
1 Like

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?

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!

If you’re doing it that way, make sure to remove the line from Start(), otherwise it will overwrite what you assign.

Done, thanks!