Enemy spawn system. I want easy enemies to spawn first then medium after that i want hard enemies to spawn nonstop. i want it a endless spawn Please [closed]

假如想象 提交于 2019-12-31 07:32:48

问题


  • 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.



来源:https://stackoverflow.com/questions/38022472/enemy-spawn-system-i-want-easy-enemies-to-spawn-first-then-medium-after-that-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!