My instantiations stop after a few spawns, but I want them to keep spawning forever thru the game

36 views Asked by At

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 i have used a off-screen instance (tried) but these enemies have moving scripts on them ..i put a off-screen object like you said but all the objects have a moving script since the enemy's point is to fly thru the screen so player tries to kill as many enemies as he can so if I take off the moving script they do spawn constantly but when I put the move script on they die and do not spawn, and again I need them to move thru the screen, so im stuck I still cant figure this one out

    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);
        }

    }

}
0

There are 0 answers