IndexOutOfRangeException:索引超出了数组的范围。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HeartManager : MonoBehaviour
{
public Image[] hearts;
public Sprite fullHeart;
public Sprite halfFullHeart;
public Sprite emptyHeart;
public FloatValue heartContainers;
// Start is called before the first frame update
void Start()
{
InitHearts();
}
public void InitHearts()
{
for (int i = 0; i < heartContainers.initialValue; i++)
{
hearts[i].gameObject.SetActive(true);
hearts[i].sprite = fullHeart;
}
}
}
我该如何解决这个问题?
Youtube、google 我仍然不知道这个错误是什么意思,我是 c# 新手,我正在观看教程。
您正在迭代的数组 -
hearts
- 长度为 hearts.Length
;您确定 heartContainers.initialValue
具有相同的值吗?
这里的一个简单修复大概是:
var hearts = this.hearts; // snapshot value to allow some internal optimizations
for (int i = 0; i < hearts.Length; i++)
{
hearts[i].gameObject.SetActive(true);
hearts[i].sprite = fullHeart;
}
如果这意味着是等价的:您可能需要检查一下!