团结产生敌人

时间:2016-06-25 09:20:03

标签: c# unity3d unityscript

目前我有产卵系统产生Easy然后中等敌人但是我得到阵列超出范围错误并且它只产生了4个敌人。我想要x20 easy(或一般数字)x20中等,然后在(简单,中等和强敌)之间随机。

这是我的代码

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

}

}

1 个答案:

答案 0 :(得分:0)

异常可能来自敌人[index],你的代码使用的索引是0,1,2。是否有3个GameObjects建议敌人[]?

spawnPoints [spawnPointIndex]可能为空,因此spawnPoints [0]将导致该异常。 spawnPoints []中至少有一个变换?

为了防止异常,您可以执行以下操作:

if ( index > 0 && index < enemy.Length && spawnPointIndex > 0 && spawnPointIndex < spawnPoints.Length )
{
    GameObject InstanceEnemies= Instantiate ( enemy[index] , spawnPoints[spawnPointIndex].position , spawnPoints[spawnPointIndex].rotation) as GameObject;

    EnemiesList.Add(InstanceEnemies);
}