【发布时间】:2016-10-27 14:36:55
【问题描述】:
- 目前我的生成系统可以生成简单的然后中等的敌人,但是我遇到了 Array out of range 错误,它只生成了 4 个敌人。我想要 x20 简单(或一般数字)x20 中等,然后在(简单、中等和硬敌人之间随机)。
这是我的代码:
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);
}
}
【问题讨论】:
-
对于非常一般的问题,请尝试 gamedev.com 或 answers.unity3d.com。本网站仅针对特定的编程问题。