Форум программистов, компьютерный форум, киберфорум
Unity, Unity3D
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.69/13: Рейтинг темы: голосов - 13, средняя оценка - 4.69
1 / 1 / 0
Регистрация: 31.03.2022
Сообщений: 28
1

Как исправить IndexOutOfRangeException: Index was outside the bounds of the array

01.04.2022, 13:14. Показов 2675. Ответов 2

Author24 — интернет-сервис помощи студентам
Возникает такая ошибка IndexOutOfRangeException: Index was outside the bounds of the array.
Character+<play>d__4.MoveNext () (at Assets/Scripts/Character.cs:21)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <d3b66f0ad4e34a55b6ef91ab84878193>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Character:Start() (at Assets/Scripts/Character.cs:14)

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Character : MonoBehaviour
{
    SelectedCharacters.Data data = new SelectedCharacters.Data();
    private int i;
    public GameObject[] AllCharacters;
    
    private void Start()
    {
        data = JsonUtility.FromJson<SelectedCharacters.Data>(PlayerPrefs.GetString("SaveGame"));
        StartCoroutine(play());
    }
 
    public IEnumerator play()
    {
            i = 0;
 
            while (AllCharacters[i].name != data.currentCharacter)
            {
                i++;
            }
            AllCharacters[i].SetActive(true);
            yield return null;
        
    }
}
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
01.04.2022, 13:14
Ответы с готовыми решениями:

IndexOutOfRangeException: Index was outside the bounds of the array
Делаю следующие, разбиваю массив и беру значения answ=&quot;Answer&lt;#&gt;Data&quot;; string b = answ.Split(new...

IndexOutOfRangeException: Index was outside the bounds of the array
Массив выходит за пределы не могу понять где , помогите пожалуйста! юнити ругается,говорит что...

System.IndexOutOfRangeException Index was outside the bounds of the array
Приветствую всех. Столкнулся с проблемой System.IndexOutOfRangeException Index was outside the...

System.IndexOutOfRangeException: Index was outside the bounds of the array
System.IndexOutOfRangeException: Index was outside the bounds of the array. at...

2
95 / 60 / 36
Регистрация: 07.08.2013
Сообщений: 241
01.04.2022, 13:47 2
у вас [i] не ограничено длинной массива
C#
1
if (i <= AllCharacters.Length) { };
0
565 / 362 / 208
Регистрация: 18.10.2019
Сообщений: 1,231
01.04.2022, 20:09 3
defoh, судя по всему возникает ситуация, когда в массиве нет объекта, который бы противоречил этому условию:
Цитата Сообщение от defoh Посмотреть сообщение
while (AllCharacters[i].name != data.currentCharacter)
Чтобы избежать ошибки, можно написать так:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    public IEnumerator play()
    {
            i = 0;
 
            while (AllCharacters[i].name != data.currentCharacter && i < AllCharacters.Length)
            {
                i++;
            }
            if(i == AllCharacters.Length)
                Debug.Log("Искомый объект отсутствует в массиве!");
            else
                AllCharacters[i].SetActive(true);
 
            yield return null;       
    }
0
01.04.2022, 20:09
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
01.04.2022, 20:09
Помогаю со студенческими работами здесь

IndexOutOfRangeException: Index was outside the bounds of the array. (ошибка в коде)
Всем привет, возникает вот такая ошибка в коде: &quot;IndexOutOfRangeException: Index was outside the...

Ошибка - Runtime exception: System.IndexOutOfRangeException: Index out of the bounds of the array
Всем привет, помогите пожалуйста решить ошибку - Runtime exception:...

Выдает такую ошибку IndexOutOfRangeException: Index was outside the bounds of the array. Бьюсь уже 3 час над решением
IndexOutOfRangeException: Index was outside the bounds of the array. SelectedCharacter.ArrowRight...

Ошибка Index was outside the bounds of the array. Как исправить?
Пытаюсь написать простую программу по выводу массива на экран. Visual Studio показывает 0 ошибок,...

Ошибка System.IndexOutOfRangeException: "Index was outside the bounds of the array"
Юнити. Есть трёхмерный массив, есть цикл. Но я не понимаю, почему на 34 строке появляется...

Ошибка System.IndexOutOfRangeException: "Index was outside the bounds of the array"
В двумерном массиве A=(a1, а2, ..., аn) отрицательные элементы, имеющие четный порядковый номер,...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
3
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru