- Home /
Remove a gameObject from remote list (Spawner)
Hello! Very new to Unity. Lover of it all.
I have an enemy spawner. I only want there to be a certain number of enemies present from each particular spawner.
void LateUpdate () {
if (On) {
}
if (enemiesList.Count < RoamingAmount)
{
if (character != null)
{
GameObject enemy = Instantiate<GameObject>(character, spawner.position, spawner.rotation);
enemiesList.Add(enemy);
var control = enemy.GetComponent<AICharacterControl>();
control.target = destination;
}
else
{
Debug.Log("No character in Spawner.");
}
}
As you can see I rely on a list to let me know whether or not there are enough enemies present. RoamingAmount is the number of enemies I want to come from the spawner at a time. The difficulty I'm having though is when my Projectile prefab destroys an enemy, I'm not sure how to get it to also remove the enemies from the Spawner's enemy list so that it'll generate more enemies. Any tips? Also, are there maybe better ways out there to accomplish this?
I've thought about assigning a "Spawner ID" to each spawner and somehow passing it on to the instantiated enemy as a property and then searching for the enemy's "parent" spawner in my onColliderEnter script but I'm not really sure how to go about extending gameObjects, or if extending's really the path I would have to take.
Any help would be appreciated!
Answer by toddisarockstar · May 16, 2017 at 07:51 PM
If this is a list you are using you can remove something from the list like this
enemiesList.remove(gameobject reference goes here);