@opt1ons

Пишет ошибку при заупуске в 42 и 17, не знаю, что делать (NullReferenceException: Object reference not set to an instance of an object)?

using System;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Controller : MonoBehaviour
{
    Button[,] buttons;
    Image[] images;

    GAME game;

    void Start()
    {
        game = new GAME(ShowBox);
  (17)      InitButtons();
        InitImage();
        ShowBox(1, 2, 3);
        game.Start();
    }

    public void ShowBox(int x, int y, int cube)
    {
        buttons[x, y].GetComponent<Image>().sprite = images[cube].sprite;
    }

    public void Click()
    {

        string name = EventSystem.current.currentSelectedGameObject.name;
        int nr = GetNumber(name);
        int x = nr % GAME.SIZE;
        int y = nr / GAME.SIZE;
        Debug.Log($"clicked {name} {x} {y}");
    }

    private void InitButtons()
    {
        buttons = new Button[GAME.SIZE, GAME.SIZE];
        for (int nr = 0; nr < GAME.SIZE * GAME.SIZE; nr++)
  (42)          buttons[nr % GAME.SIZE, nr / GAME.SIZE] =
                GameObject.Find($"Button({nr})").GetComponent<Button>();
    }

    private void InitImage()
    {
        images = new Image[GAME.CUBE];
        for (int j = 0; j < GAME.CUBE; j++)
            images[j] = GameObject.Find($"Image ({j})").GetComponent<Image>();
    }

    private int GetNumber(string name)
    {
        Regex regex = new Regex("\\((\\d+)\\)");
        Match match = regex.Match(name);
        if (!match.Success)
            throw new System.Exception("Unrecognized object name");
        Group group = match.Groups[1];
        string number = group.Value;
        return Convert.ToInt32(number);
    }
}

У меня только одна картинка, которая должна выводиться на кнопки ( вдруг в этом ошибка, может быть как-то по-другому написать )
5ec253e9d0590494441717.png
5ec253f586a89891407610.png
  • Вопрос задан
  • 92 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы