IndexOutOfRangeException: Index was outside the bounds of the array.

Hello friends, i have this error in unity 2019.2.15:

IndexOutOfRangeException: Index was outside the bounds of the array.
LoadingScene.RandImg () (at Assets/Scripts/LoadingScene.cs:63)
LoadingScene.Start () (at Assets/Scripts/LoadingScene.cs:31)

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class LoadingScene : MonoBehaviour
{
    public Text LOADINGTEXT;

    public Text LOADINGTEXT_X;
    public bool isForEver;

    public GameObject explain_ImgObj;
    public GameObject explain_ImgObj2;

    public Sprite[] LLoadingImg;
    public Sprite[] LLoadingImg2;
    private int iLastR;
    private int iLastR2;

    public void candy2()
    {
    }

  
    private void Start()
    {

      
        Singleton<DataManager>.Instance.SetLanguageFont(LOADINGTEXT, Singleton<DataManager>.Instance.LanguageColor[0], Singleton<DataManager>.Instance.LanguageColor[0], 0f, "LOADINGTEXT");
        StartCoroutine(StartLoading());
        RandImg();
        hideBanner();
        HideCenterBanner();
    }

    public void hideBanner()
    {

    }

    public void HideCenterBanner()
    {

    }

    public void RandImg()
    {
        Singleton<DataManager>.Instance.SetLanguageFont(LOADINGTEXT, Singleton<DataManager>.Instance.LanguageColor[0], Singleton<DataManager>.Instance.LanguageColor[0], 0f, "LOADINGTEXT");
        int num = Random.Range(0, 5);
        while (iLastR == num)
        {
            num = Random.Range(0, 5);
        }
        iLastR = num;
        int num2 = Random.Range(0, 5);
        while (iLastR2 == num2)
        {
            num2 = Random.Range(0, 5);
        }
        iLastR2 = num2;
        DataManager.LoadingSceneImageIndex = num;
        DataManager.LoadingSceneImageIndex = num2;
        explain_ImgObj.gameObject.GetComponent<Image>().sprite = LLoadingImg[num];
        explain_ImgObj2.gameObject.GetComponent<Image>().sprite = LLoadingImg2[num2];
        Singleton<DataManager>.Instance.SetLanguageFont(LOADINGTEXT_X, Singleton<DataManager>.Instance.LanguageColor[0], Singleton<DataManager>.Instance.LanguageColor[1], 0.16f, "LOADINGTEXT" + (num + 1));
    }




private IEnumerator StartLoading()
    {
        if ((bool)MapController.controller)
        {
            MapController.controller.gameObject.SetActive(value: false);
        }
        yield return new WaitForSeconds(0.1f);
        UnityEngine.Debug.Log("Lihai==== 111111111111");
        EnumSceneType ChangeSceneType = Singleton<DataManager>.Instance.ChangeSceneType;
        string sceneName = Singleton<SceneManager>.Instance.GetSceneName(ChangeSceneType);
        UnityEngine.Debug.Log("Lihai==== 2222222222");
        UI.Instance.ClearUI();
        UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
        UnityEngine.Debug.Log("Lihai==== 3333333");
        if ((bool)LoadingSceneUI.action && Singleton<DataManager>.Instance.ChangeSceneType != EnumSceneType.LevelScene)
        {
            LoadingSceneUI.action.HideLoadingSceneUI();
        }
    }
}

I don’t know what is wrong. Can anyone help me please?
Thanks.

The error is fairly clear. IndexOutOfRange means you’re trying to access an index that doesn’t exist, thus out of range.

So if your LLoadingImg array only has 2 items in it but you try to access index slot 4, it can’t find anything.

Unless you know you have 5 items in it, it’s better to do this

Random.Range(0, LLoadingImg.Length);