Enemy spawn system. I want easy enemies to spawn f

2019-03-06 18:54发布

问题:

  • Currently i have spawning system that spawns Easy then medium enemies but I'm getting Array out of range error and it's only spawning 4 of the enemies. i want x20 easy (or general number) x20 medium and then random between (easy,medium and hard enemies.)

This is my Code:

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

public class Test : MonoBehaviour
{

public GameObject[] enemy; 

public Transform[] spawnPoints;         

private float timer = 2;


int index = 0 ;

int wave = 0;

List <GameObject> EnemiesList = new List<GameObject>();

private int enemyCount=20;


void Update()
{
    timer -= Time.deltaTime;

    if (timer <= 0 && wave < 6)
    {
        timer = 3;

        if (wave != 0 &&  wave % 2 == 0)
        {
            index ++ ;
        }

        EnemySpawner();

        wave++;
    }

}

void Spawn ()
{

    for (int i = 0; i<enemyCount;i++)
    {
        Invoke("EnemySpawner" , i + 2);
    }
}

void EnemySpawner ()
{
    int spawnPointIndex = Random.Range (0, spawnPoints.Length);

    GameObject InstanceEnemies= Instantiate ( enemy[index] , spawnPoints[spawnPointIndex].position , spawnPoints[spawnPointIndex].rotation) as GameObject;

    EnemiesList.Add(InstanceEnemies);

}

public void Remove (GameObject anything)
{
    EnemiesList.Remove (anything);
}

}

回答1:

I believe you need to set your enemyCount variable to be set to the length of your array. What if you have 15 enemies in the array and enemyCount is still 20? Sounds like an IndexOutOfRangeException to me.