每波增加敌人的生命值

Increase enemy health every wave

正如标题所说,我想在每一波之后增加我的敌人生命值,但是在第一波之后我得到“IndexOutOfRangeException:索引超出了数组的范围。”错误

注意:我还是个初学者,这是我从事的第一个项目,我遵循了这个脚本的教程(我自己也写了一些东西)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class WaveSpawner : MonoBehaviour
{
    public enum SpawnState { SPAWNING, WAITING, COUNTING };
    public int waveNumber = 0;
    [SerializeField] public TMP_Text waveNumberTextValue;
    public bool waveEnded = false;

    [System.Serializable]
    public class Wave
    {
        public string name;
        public Transform enemy;
        public int count;
        public float rate;
    }

    public Wave[] waves;
    private int nextWave = 0;

    public Transform[] spawnPoints;

    public float timeBetweenWaves = 5f;
    private float waveCountdown;

    private float searchCountdown = 1f;

    private SpawnState state = SpawnState.COUNTING;


    void Start()
    {

        waveNumber = 1;
        waveCountdown = timeBetweenWaves;
    }
    
    public void EnemyHpIncrease()
    {
        if (waveEnded == true)
        {
            if(waveNumber <= 10)
            {
                FindObjectsOfType<EnemyHealth>()[0].enemyHealth += 100f;
            }
            else
            {
                FindObjectsOfType<EnemyHealth>()[0].enemyHealth *= 1.1f;
            }
        }
    }

    void Update()
    {
        waveNumberTextValue.text = "Wave " + waveNumber.ToString("0");
        if(state == SpawnState.WAITING)
        {
            if(!EnemyIsAlive())
            {
                WaveCompleted();
                Debug.Log("hp increased by 100hp");
                waveEnded = true;
                waveNumber++; 
                
                
            }
            else
            {
                return;
            }
        }

        if(waveCountdown <= 0)
        {
            if(state != SpawnState.SPAWNING)
            {
                StartCoroutine(SpawnWave(waves[nextWave]));
            }
        }
        else
        {
            waveCountdown -= Time.deltaTime;
        }
    }

    public void WaveCompleted()
    {
        waveEnded = true;
        EnemyHpIncrease();
        state = SpawnState.COUNTING;
        waveCountdown = timeBetweenWaves;

        if(nextWave + 1 > waves.Length - 1)
        {
            nextWave = 0;
            Debug.Log("ALL WAVES COMPLETED!");
        }
            else
        {
            nextWave++;
        }
    }

    bool EnemyIsAlive()
    {
        searchCountdown -= Time.deltaTime;
        if(searchCountdown <= 0f)
        {
            searchCountdown = 1f;
            if(GameObject.FindGameObjectWithTag("Enemy") == null)
            {
                
                return false;
            }
        }
        return true;
    }

    IEnumerator SpawnWave (Wave _wave)
    {
        Debug.Log("Spawning Wave" + _wave.name);
        state = SpawnState.SPAWNING;
        waveEnded = false;

        for (int i = 0; i < _wave.count; i++)
        {
            SpawnEnemy(_wave.enemy);
            yield return new WaitForSeconds( 1f/_wave.rate );
        }

        state = SpawnState.WAITING;
        yield break;
    }

    void SpawnEnemy(Transform _enemy)
    {
        Debug.Log("Spawning Enemy" + _enemy.name);
        Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
        Instantiate (_enemy, _sp.position, _sp.rotation);
    }
}

我试着把 debug.log 放在任何地方,但在 WaveCompleted() 之后它不再显示了;在

                WaveCompleted();
                Debug.Log("hp increased by 100hp");
                waveEnded = true;
                waveNumber++; 
         

错误:

IndexOutOfRangeException: Index was outside the bounds of the array.
WaveSpawner.EnemyHpIncrease () (at Assets/scripts/WaveSpawner.cs:48)
WaveSpawner.WaveCompleted () (at Assets/scripts/WaveSpawner.cs:93)
WaveSpawner.Update () (at Assets/scripts/WaveSpawner.cs:65)

IndexOutOfRangeException 在第 48 行:

IndexOutOfRangeException: Index was outside the bounds of the array.
WaveSpawner.EnemyHpIncrease () (at Assets/scripts/WaveSpawner.cs:48) <-- line number here

将您的代码复制到 Notepad++ 中,第 48 行如下所示:

FindObjectsOfType<EnemyHealth>()[0].enemyHealth += 100f;

代码假设 FindObjectsOfType<> 方法的结果是 returning 一个至少包含一个元素的数组。我假设这种情况下的结果是一个空数组,所以位置 0 没有元素。

如果 FindObjectsOfType 的 return 值应该是一个对象,而您只想更新一个对象,那么您只能在数组中至少有一个元素时才执行该操作。如果要更改所有 returned 对象,则需要使用 foreach 循环遍历数组。