- Home /
Spawner with max enemies
Hi, I'll try keep this as detailed as I can but short.
I'm trying to make a spawner that has max amount of enemies it can spawn say 10, I got it to the point were it will spawn 1 enemy and if he dies it spawns another but when it comes to spawning more than 1 thats were it gets annoying.
var enemy1 : Transform;
function Update () {
var enemyTarget = GameObject.FindWithTag("Enemy");
if( enemyTarget == null )
{
SpawnEnemy();
}
}
function SpawnEnemy()
{
Instantiate(enemy1, transform.position, transform.rotation);
}
This is the code I've got, I looked all over the internet and I tried Invoke and that didn't work, I tried While(true) but that just failed and crashed Unity o_O. So I'm at a lost with this :(.
Answer by gegc · Aug 21, 2012 at 03:44 PM
Right now you have an enemy instance stored in a variable, and when it is null you spawn another one. So, this code will only ever spawn one enemy at a time. There's really any number of ways you can solve this problem because it's just a programming problem, but here's one way you can do it for the sake of example:
//This is a behaviour to stick in your enemy prefab
public class SpawnedObject : Monobehaviour {
public ObjectSpawner homeSpawn;
void OnDestroy() {
homeSpawn.spawnCount--;
}
}
//This is the spawner
public class ObjectSpawner : MonoBehaviour {
//object you want to spawn
SpawnedObject objectPrefab;
//maximum allowed number of objects - set in the editor
public int maxObjects = 1;
//number of objects currently spawned
public int spawnCount = 0;
void Update() {
if(maxObjects>spawnCount) {
SpawnEnemy();
}
}
void SpawnEnemy() {
SpawnedObject obj =
((GameObject)(Instantiate(objectPrefab,transform.position,transform.rotation)))
.GetComponent<SpawnedObject>();
obj.homeSpawn = this;
spawnCount++;
}
}
Thank you for the reply, for some reason both scripts keep thorwing the error "The type or namespace name `$$anonymous$$onobehaviour' could not be found". I'm not 100% why because the script names match the public class and I have the using UnityEngine and using System.Collections at the top of the page.
Answer by Paulo-Henrique025 · Aug 21, 2012 at 03:48 PM
Take a look, I'm in a rush right now and can't properly explain the code but try it and see what happens when you delete an object.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnTest : MonoBehaviour {
public List<GameObject> spawnList;
// Use this for initialization
void Start ()
{
spawnList = new List<GameObject>();
InvokeRepeating("CheckAlive", 1, 1);
//Populate for the first time
for (int i = 0; i < 10; i++)
{
spawnList.Add(new GameObject(Time.timeSinceLevelLoad.ToString()));
}
}
void CheckAlive()
{
for (int i = 0; i < spawnList.Count; i++)
{
if(!spawnList[i])
{
spawnList[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
spawnList[i].name = Time.timeSinceLevelLoad.ToString();
}
}
}
}
Thank You for the reply. This works but when I try hunt for the enemies I cant find them. I looked in the scene view and I can see where they should be with the move objects tool but there is no mesh or anything of the enemy :S
Ah, I've just created a simple object, it's just a cube, you must Instantiate your enemy prefab ins$$anonymous$$d. To try my script just delete one object from the hierarchy and it will be replaced. The InvokeRepeating method calls the function that replaces dead objects every 1s, there is no need call that in the update.
Ah, just done the same thing and it works, one thing tho when it spawns its just a normal cube ins$$anonymous$$d of a say clone of the cube prefab I done.
As I said, just make a public GameObject pointing to your prefab variable and instantiate it ins$$anonymous$$d of my simple cube creation.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnTest : $$anonymous$$onoBehaviour {
public GameObject myEnemy;
public List<GameObject> spawnList;
// Use this for initialization
void Start ()
{
spawnList = new List<GameObject>();
InvokeRepeating("CheckAlive", 1, 1);
//Populate for the first time
for (int i = 0; i < 10; i++)
{
// Changed here AGAIN
spawnList.Add(Instantiate(myEnemy) as GameObject);
}
}
void CheckAlive()
{
for (int i = 0; i < spawnList.Count; i++)
{
if(!spawnList[i])
{
// Also changed here AGAIN
spawnList[i] = Instantiate(myEnemy) as GameObject;
// spawnList[i].name = Time.timeSinceLevelLoad.ToString(); //Commented so it stop changing the name
}
}
}
}
Ah ok, I tried the code again and it keep throwing an "A namespace can only contain types and namespace declarations" error on the Public GameObject myEnemy and not sure why. Sorry to bother you with this I'm not overly sure with C#....I should learn it to be honest lol
Your answer
![](https://koobas.hobune.stream/wayback/20220613075954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating prefab at child (spawnlocations are arrays) 2 Answers
instanstiate Spawns more then one objec 0 Answers
Network.Instantiate players at each spawnpoint? Don't use the same spawnpoint? 1 Answer
Spawn from array 2 Answers
How to make my new Instances spawn in the right direction? 1 Answer