Скрипт. Цикл. NullReferenceException

Программирование на Юнити.

Скрипт. Цикл. NullReferenceException

Сообщение Nekokoneko 25 ноя 2019, 10:41

Доброго времени суток.

Есть скрипт. Работает.
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Select : MonoBehaviour
{
    public Button[] Button;

    int isSold0;
    int isSold1;
    int isSold2;
    int isSold3;
    int isSold4;
    int isSold5;
    int isSold6;
    int isSold7;
    int isSold8;
    int isSold9;
    int isSold10;
    int isSold11;
    int isSold12;
    int isSold13;
    int isSold14;
    int isSold15;
    int isSold16;
    int isSold17;

    void Start()
    {
        isSold0 = PlayerPrefs.GetInt("isSold0");
        isSold1 = PlayerPrefs.GetInt("isSold1");
        isSold2 = PlayerPrefs.GetInt("isSold2");
        isSold3 = PlayerPrefs.GetInt("isSold3");
        isSold4 = PlayerPrefs.GetInt("isSold4");
        isSold5 = PlayerPrefs.GetInt("isSold5");
        isSold6 = PlayerPrefs.GetInt("isSold6");
        isSold7 = PlayerPrefs.GetInt("isSold7");
        isSold8 = PlayerPrefs.GetInt("isSold8");
        isSold9 = PlayerPrefs.GetInt("isSold9");
        isSold10 = PlayerPrefs.GetInt("isSold10");
        isSold11 = PlayerPrefs.GetInt("isSold11");
        isSold12 = PlayerPrefs.GetInt("isSold12");
        isSold13 = PlayerPrefs.GetInt("isSold13");
        isSold14 = PlayerPrefs.GetInt("isSold14");
        isSold15 = PlayerPrefs.GetInt("isSold15");
        isSold16 = PlayerPrefs.GetInt("isSold16");
        isSold17 = PlayerPrefs.GetInt("isSold17");

        if (isSold0 == 1) Button[0].interactable = true;
        else Button[0].interactable = false;
        if (isSold1 == 1) Button[1].interactable = true;
        else Button[1].interactable = false;
        if (isSold2 == 1) Button[2].interactable = true;
        else Button[2].interactable = false;
        if (isSold3 == 1) Button[3].interactable = true;
        else Button[3].interactable = false;
        if (isSold4 == 1) Button[4].interactable = true;
        else Button[4].interactable = false;
        if (isSold5 == 1) Button[5].interactable = true;
        else Button[5].interactable = false;
        if (isSold6 == 1) Button[6].interactable = true;
        else Button[6].interactable = false;
        if (isSold7 == 1) Button[7].interactable = true;
        else Button[7].interactable = false;
        if (isSold8 == 1) Button[8].interactable = true;
        else Button[8].interactable = false;
        if (isSold9 == 1) Button[9].interactable = true;
        else Button[9].interactable = false;
        if (isSold10 == 1) Button[10].interactable = true;
        else Button[10].interactable = false;
        if (isSold11 == 1) Button[11].interactable = true;
        else Button[11].interactable = false;
        if (isSold12 == 1) Button[12].interactable = true;
        else Button[12].interactable = false;
        if (isSold13 == 1) Button[13].interactable = true;
        else Button[13].interactable = false;
        if (isSold14 == 1) Button[14].interactable = true;
        else Button[14].interactable = false;
        if (isSold15 == 1) Button[15].interactable = true;
        else Button[15].interactable = false;
        if (isSold16 == 1) Button[16].interactable = true;
        else Button[16].interactable = false;
        if (isSold17 == 1) Button[17].interactable = true;
        else Button[17].interactable = false;
    }

    void Update() {}

 }
 


Я решил его сократить при помощи цикла
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ArmorySelect : MonoBehaviour
{
    public Button[] WeaponButton;

    int[] isSold;

    void Start()
    {
        for (int i = 0; i < 17; i++)
        {
            isSold[i] = PlayerPrefs.GetInt("isSold" + i.ToString());

            if (isSold[i] == 1)
                WeaponButton[i].interactable = true;
            else
                WeaponButton[i].interactable = false;
        }
    }

    // Update is called once per frame
    void Update() {}

 }
 


Выдает NullReferenceException на int[] isSold;

Пробовал через [Serializable] int[] isSold;
Тогда выдает ошибку на цикл IndexOutOfRangeException: Array index is out of range

Подскажите, как это исправить?
Nekokoneko
UNец
 
Сообщения: 13
Зарегистрирован: 29 июн 2018, 17:18

Re: Скрипт. Цикл. NullReferenceException

Сообщение 1max1 25 ноя 2019, 10:48

1. Не присвоен экземпляр массива int[] isSold = new int[18];
2. У тебя элементов 18, а не 17 for (int i = 0; i < isSold.Length; i++)
3. Здесь вообще массив не нужен:
Синтаксис:
Используется csharp
        for (int i = 0; i < WeaponButton.Length; i++)
        {
            WeaponButton[i].interactable = PlayerPrefs.GetInt("isSold" + i) == 1;
        }
Аватара пользователя
1max1
Адепт
 
Сообщения: 5505
Зарегистрирован: 28 июн 2017, 10:51

Re: Скрипт. Цикл. NullReferenceException

Сообщение Nekokoneko 25 ноя 2019, 10:54

Благодарю.
Nekokoneko
UNец
 
Сообщения: 13
Зарегистрирован: 29 июн 2018, 17:18


Вернуться в Скрипты

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 12