Reputation: 1
My instantiations stop after 1 or 2 spawns, but I want them to keep spawning forever thru the game..
It's because I have the destroy piece of code in this script, but what can I do I need them to die after collision or there will be hundreds on screen like before. As usual when dead an enemy must die and disappear. So what do I do? The destroy code is destroying the entire enemy player /prefab not just the dead instance like I want.
I want the enemies to keep spawning but of course when killed by player they must disappear.
2d game I have this script on the enemy prefab and in scene enemies
I want the enemy on screen to die one by one when collision but the destroy is killing the entire object / prefab I think and it will not instantiate /spawn any more
I do have a few scripts on the enemy here is the instantiation script
using UnityEngine; using System.Collections;
public class spn2 : MonoBehaviour {
GameObject Enemy;
//public GameObject EasyEnemey;
public GameObject MediumEnemey;
public GameObject HardEnemey;
public Transform[] SpawnPoints;
public float TimeBetweenSpawns;
public int NumberOfEnemiesToSpawn;
public int NumberOfMediumEnemiesToSpawn;
public float EasyChance;
public float MediumChance;
public float HardChance;
private int waveNumber;
private float spawnTimer;
private int numberOfEnemies;
private int numberOfMediumEnemies;
// Use this for initialization
void Start()
{
//this below is the time to spawn so if 4 , every 4 seconds 1 will spawn etc
this.spawnTimer = 3.0f;
this.waveNumber = 0;
float totalChance = this.EasyChance + this.MediumChance + this.HardChance;
if(Mathf.Abs(totalChance-1.0f)>0.0001f) {
Debug.LogWarning("Warning: The chances should add up to 1.0 ("+totalChance+" currently)");
}
}
// Update is called once per frame
void Update()
{
this.spawnTimer -= Time.deltaTime;
if(this.spawnTimer<=0.0f)
{
Transform spawnPoint = this.SpawnPoints[Random.Range(0, this.SpawnPoints.Length)];
Vector2 spawnPos = spawnPoint.position;
Quaternion spawnRot = spawnPoint.rotation;
switch(this.waveNumber)
{
case 0:
//Instantiate(EasyEnemey, spawnPos,spawnRot);
Instantiate(Resources.Load(Enemy) as GameObject, spawnPos, spawnRot);
this.numberOfEnemies++;
if(this.numberOfEnemies>=this.NumberOfEnemiesToSpawn)
{
this.waveNumber++;
}
break;
case 1:
Instantiate(MediumEnemey, spawnPos, spawnRot);
this.numberOfMediumEnemies++;
if (this.numberOfMediumEnemies >= this.NumberOfMediumEnemiesToSpawn)
{
this.waveNumber++;
}
break;
case 2:
float randomFloat = Random.value;
if(randomFloat<this.EasyChance)
{
Instantiate(Enemy, spawnPos, spawnRot);
}
else if(randomFloat<this.EasyChance+this.MediumChance)
{
Instantiate(MediumEnemey, spawnPos, spawnRot);
}
else
{
Instantiate(HardEnemey, spawnPos, spawnRot);
}
break;
}
this.spawnTimer = this.TimeBetweenSpawns;
Destroy (gameObject, .7f);
}
}
}
Upvotes: 0
Views: 1203
Reputation: 3020
You are instantiating the prefab as your actual object, so when you delete your instance, you are deleting your prefab. Try this code for the initialization instead. This will create a copy of the prefab and load it as an instance.
GameObject enemy = Instantiate(Resources.Load("Name of your prefab here") as GameObject, spawnPos, spawnRot);
For your other problem, where your things start spawning, let's take a look at your code. I've removed some things that aren't relevant, but I just copy and pasted from your stuff. Look at what your code is doing.
Every update (called by Unity continuously, we can approximate as once every frame), we call this method, Update. It subtracts some time from SpawnTimer, so let's say your spawn timer was 4 seconds, and Time.DeltaTime for example, is 1 second (it isn't, but it's an example).
So your update will run 4 times, that's great, but what happens now? Suddenly, your spawn timer is 0, and this condition
if(this.spawnTimer<=0.0f)
evaluates to True, so we are ready to instantiate an enemy. Great. We go through the switch statement, select our enemy, create them, and then we call Destroy, which immediately deletes our gameObject.
public class spn2 : MonoBehaviour {
//variables here that I removed
//removed start method
// Update is called once per frame
void Update()
{
this.spawnTimer -= Time.deltaTime;
if(this.spawnTimer<=0.0f)
{
//instantiation stuff;
{
case 0:
Instantiate(Resources.Load(Enemy) as GameObject, spawnPos, spawnRot);
break;
case 1:
Instantiate(MediumEnemey, spawnPos, spawnRot);
break;
case 2:
//instantiation logic
break;
}
this.spawnTimer = this.TimeBetweenSpawns;
Destroy (gameObject, .7f); <-- Destroy is here? This is your problem!
}
}
}
Upvotes: 3