- Home /
Need help with enemy respawn script
Hello, this is the script. Basically, it's mixed with the enemy health script so it's easier for me, I want to spawn more enemies as soon as the group that spawned earlier dies, for example, the game starts with only one enemy, as soon as he's dead, I want two more enemies to spawn and when they are both dead I want to spawn three and so on. The problem with this script is that I want them to spawn on random positions but they always spawn on the same position and one at the time. Looking at the code the group of enemy would spawn when just one is killed and that's wrong, so, in short:
1) How can I spawn a group of enemies when the group that spawned earlier is dead? 2) How can I spawn the enemies in random positions?
This is the code I made
public var damage = 15;
private var health = 100;
private var enemyNumber = 2;
function Start () {
}
function Update () {
if(health < 1) {
animation.Play("death");
Destroy(gameObject, 0.31);
}
Debug.Log(health);
if(health < 0) {
health = 0;
respawn();
}
}
function OnCollisionEnter (bulletCollision : Collision) {
if(bulletCollision.gameObject.tag == "bullet") {
health -= damage;
Debug.Log("Hit enemy");
}
}
function respawn() {
var Enemy = GameObject.FindGameObjectWithTag("Enemy");
var enemySpawns = GameObject.FindGameObjectsWithTag("spawns");
for(var i = 0; i < enemyNumber; i++) {
var spawn = Random.Range(0, enemySpawns.Length);
Instantiate(Enemy, enemySpawns[spawn].transform.position, transform.rotation);
}
enemyNumber++;
}
EDIT: Just found out that more than one enemy spawn when one is killed, the problem is that by spawning on the same place they (I'm using cubes at the moment), let's say sum up and it seems like there is only one cube. As I said above, I want them to spawn when the group that spawned earlier is gone and I want them to spawn to random places.
EDIT2: The code remains the same but now the random spawns are working and it spawns more than one enemy at the time. Still, it spawns too many enemies and not when the group is dead.
Print out the positions which are stored inside enemySpawns. Are they different positions? Are there multiple values?
As I said in the second edit random spawns now are working. Still, too many enemies spawn when one is killed.
Answer by wijesijp · Apr 25, 2014 at 10:29 AM
If the code you post is enemy code then the respawn logic is wrong.
If you had 2 enemies then when each one gets killed will spawn 2 enemies. So you will have 4 enemies when 2 enemies get killed not 3 as you wanted. Also enemyNumber is a private variable it will not keep on increasing in the next enemy enemyNumber is still 2.
The best way to handle is not to put respawn logic inside enemy class. Create another class that handles enemy spawning (say EnemyMaker). When enemy get killed inform EnemyMaker that it got killed. When all the enemies get killed EnemyMaker can respawn next set of enemies.
public class EnemyMaker : MonoBehaviour
{
public static EnemyMaker current;
public GameObject enemyPrefab;
private int enemyCount = 2;
private int enemyForLevel = 2;
void Awake()
{
current = this;
}
void Start()
{
Respawn();
}
void Respawn()
{
enemyCount = enemyForLevel;
// respon code ....
}
public void EnemyIsDead()
{
enemyCount--;
}
void Update()
{
if (enemyCount == 0)
{
enemyForLevel++;
Respawn();
}
}
}
public class Enemy : MonoBehaviour
{
private float health;
void OnCollisionEnter(Collision bulletCollision)
{
// code to handle health
if (health <= 0)
{
EnemyMaker.current.EnemyIsDead();
Destroy(gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
problem extracting transform from an array 1 Answer
Instantiate random object from Array 2 Answers
How do I animate my enemy randomly with time? 1 Answer
Respawned with Camera problem 2 Answers
Enemy deals damage to player on contact 2 Answers