本文介绍了敌人生成系统.我要先让容易的敌人产生,然后再中度产生,然后我要让坚硬的敌人不停地产生.我想要它无尽的产卵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 目前,我有一个生成系统,可以生成简单然后中等的敌人,但是我遇到了数组超出范围的错误,并且仅生成了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);
}
}
推荐答案
我相信您需要将enemyCount
变量设置为数组的长度.如果数组中有15个敌人并且enemyCount
仍然是20个怎么办?对我来说,这听起来像是IndexOutOfRangeException.
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.
这篇关于敌人生成系统.我要先让容易的敌人产生,然后再中度产生,然后我要让坚硬的敌人不停地产生.我想要它无尽的产卵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!