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.