如何修复 Unity 中的数组索引超出范围错误?

我正在 Unity 中制作幻灯片。我有 2 个场景,每个场景都设置了一个充满图像的数组。当我按下右箭头键时,我可以遍历数组,在此过程中显示该索引中的每个图像。一旦到达当前场景中数组的末尾,我还可以通过点击右箭头键移动到项目中的下一个场景。到现在为止还挺好。

当我试图在幻灯片中向后移动时,问题就出现了。我可以通过按左箭头键轻松地在当前场景中的阵列中的图像中向后跳转,但是当我尝试移回前一个场景时,或者我在阵列的开头并按左箭头键,我遇到了一个错误:

数组索引超出范围

我有点明白计算机在说什么——我试图访问一个不存在的数组的索引,但我对如何解决这个问题一无所知。

代码

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using UnityEngine.SceneManagement;


public class LoadImage : MonoBehaviour {


[SerializeField] private Image newImage;

[SerializeField] Image[] nextImage;

int currentImageIndex = 0;


// Use this for initialization

void Start () {


}


// Update is called once per frame

void Update () {

    GetNextImage();


private Image GetNextImage()

{

    if (Input.GetKeyDown(KeyCode.RightArrow) && currentImageIndex < nextImage.Length)

    {

        newImage = nextImage[currentImageIndex];

        newImage.enabled = true;

        currentImageIndex++;

    }

    else if (Input.GetKeyDown(KeyCode.RightArrow) && currentImageIndex == nextImage.Length)

    {

        LoadNextScene();

    }


    else if (Input.GetKeyDown(KeyCode.LeftArrow) && currentImageIndex <= nextImage.Length)

    {

        Debug.Log("poop.");

        newImage = nextImage[currentImageIndex - 1];  //<--- I think this is 

        newImage.enabled = false;                     //     the problem 

                                                      //     child.

        currentImageIndex--;                          

    } 

    else if (Input.GetKeyDown(KeyCode.LeftArrow) && currentImageIndex == nextImage.Length - nextImage.Length)

    {

        LoadPreviousScene();

    }

        return newImage;

}

所以重申:我按向右箭头键一次一个地浏览图像数组。到达数组的末尾后,我再按一次向右箭头键,然后我将被带到项目中的下一个场景。但是,一旦我进入下一个场景,我就无法返回上一个场景,因为“数组索引超出范围”错误 - 我的 LoadPreviousScene() 方法没有被调用。


当我在数组的第一个索引上时,我希望能够混合左箭头键并被扔回前一个场景。


SMILET
浏览 847回答 1
1回答

江户川乱折腾

问题是由于这种情况:else if (Input.GetKeyDown(KeyCode.LeftArrow) && currentImageIndex <= nextImage.Length)当你这样做时,newImage = nextImage[currentImageIndex - 1];你需要确保它currentImageIndex大于 0。以下是我将如何编写您的方法:private Image GetNextImage(){&nbsp; &nbsp; if (Input.GetKeyDown(KeyCode.RightArrow))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(currentImageIndex < nextImage.Length)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newImage = nextImage[currentImageIndex++];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newImage.enabled = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadNextScene();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else if (Input.GetKeyDown(KeyCode.LeftArrow))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(currentImageIndex > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newImage = nextImage[currentImageIndex--];&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newImage.enabled = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoadPreviousScene();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return newImage;}注意我已经将键和索引之间的条件分开了,因为两次测试相同的条件没有多大意义。
打开App,查看更多内容
随时随地看视频慕课网APP