O
O
opt1ons2020-05-18 11:16:01
Unity
opt1ons, 2020-05-18 11:16:01

Writes an error when starting at 42 and 17, I don’t know what to do (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);
    }
}

I have only one picture, which should be displayed on the buttons (suddenly this is a mistake, maybe write something differently)
5ec253e9d0590494441717.png
5ec253f586a89891407610.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question