亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復 Unity 中的數組索引超出范圍錯誤?

如何修復 Unity 中的數組索引超出范圍錯誤?

C#
SMILET 2021-12-05 17:08:46
我正在 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 initializationvoid Start () {}// Update is called once per framevoid 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() 方法沒有被調用。當我在數組的第一個索引上時,我希望能夠混合左箭頭鍵并被扔回前一個場景。
查看完整描述

1 回答

?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

問題是由于這種情況:


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

當你這樣做時,newImage = nextImage[currentImageIndex - 1];你需要確保它currentImageIndex大于 0。


以下是我將如何編寫您的方法:


private Image GetNextImage()

{

    if (Input.GetKeyDown(KeyCode.RightArrow))

    {

        if(currentImageIndex < nextImage.Length)

        {

            newImage = nextImage[currentImageIndex++];

            newImage.enabled = true;

        } 

        else

        {

            LoadNextScene();

        }

    }

    else if (Input.GetKeyDown(KeyCode.LeftArrow))

    {

        if(currentImageIndex > 0)

        {

            newImage = nextImage[currentImageIndex--];  

            newImage.enabled = false;                     

        }

        else

        {

            LoadPreviousScene();

        }

    } 

    return newImage;

}

注意我已經將鍵和索引之間的條件分開了,因為兩次測試相同的條件沒有多大意義。


查看完整回答
反對 回復 2021-12-05
  • 1 回答
  • 0 關注
  • 1086 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號