第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何修復(fù) Unity 中的數(shù)組索引超出范圍錯誤?

如何修復(fù) Unity 中的數(shù)組索引超出范圍錯誤?

C#
SMILET 2021-12-05 17:08:46
我正在 Unity 中制作幻燈片。我有 2 個場景,每個場景都設(shè)置了一個充滿圖像的數(shù)組。當(dāng)我按下右箭頭鍵時,我可以遍歷數(shù)組,在此過程中顯示該索引中的每個圖像。一旦到達(dá)當(dāng)前場景中數(shù)組的末尾,我還可以通過點擊右箭頭鍵移動到項目中的下一個場景。到現(xiàn)在為止還挺好。當(dāng)我試圖在幻燈片中向后移動時,問題就出現(xiàn)了。我可以通過按左箭頭鍵輕松地在當(dāng)前場景中的陣列中的圖像中向后跳轉(zhuǎn),但是當(dāng)我嘗試移回前一個場景時,或者我在陣列的開頭并按左箭頭鍵,我遇到了一個錯誤:數(shù)組索引超出范圍我有點明白計算機(jī)在說什么——我試圖訪問一個不存在的數(shù)組的索引,但我對如何解決這個問題一無所知。代碼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;}所以重申:我按向右箭頭鍵一次一個地瀏覽圖像數(shù)組。到達(dá)數(shù)組的末尾后,我再按一次向右箭頭鍵,然后我將被帶到項目中的下一個場景。但是,一旦我進(jìn)入下一個場景,我就無法返回上一個場景,因為“數(shù)組索引超出范圍”錯誤 - 我的 LoadPreviousScene() 方法沒有被調(diào)用。當(dāng)我在數(shù)組的第一個索引上時,我希望能夠混合左箭頭鍵并被扔回前一個場景。
查看完整描述

1 回答

?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗 獲得超5個贊

問題是由于這種情況:


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

當(dāng)你這樣做時,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;

}

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


查看完整回答
反對 回復(fù) 2021-12-05
  • 1 回答
  • 0 關(guān)注
  • 1104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號