IndexOutOfRangeException: Index was outside the bounds of the array

При клике по объекту, я возвращаю id объекта в GameManager. Далее мне необходимо достать элемент массива по id. По какой-то причине мне выдает ошибку о выходе за пределы массива.

метод OnPointerClick обрабатывает нажатие и вызывает метод в GameManager передавая id данного объекта

public class Node : Coord, IPointerClickHandler
{
    int id;
    public int Id
    {
        get => id;
        set => id = value;
    }
    [SerializeField] private Game game;
    private SpriteRenderer spRenderer;
    private Sprite sprite;

    public Sprite Sprite
    {
        get => sprite;
    }

    public void OnPointerClick(PointerEventData pointerEventData)
    {
        if (pointerEventData.button == PointerEventData.InputButton.Left)
        {
            game.MoveNode(Id);
        }
    }
}

В GameManager содержится массив idBlocks типа int, данный массив содержит айдишники. В методе MoveNode, я желаю получить элемент массива idBlocks по id. В методе CreateObject, я заполняю массив idBlocks айдишниками

public class Game : MonoBehaviour
{
    [SerializeField] private Transform pointsParent;

    [SerializeField] private GameObject prefabNode;
    private int[] idBlocks;
    private GameObject[] gameObjects;

    [SerializeField] private Sprite[] sprites;

    private GameObject obj;
    public Texture2D texture;
    private Node node;

    private int x = 4;
    private int y = 4;

    private void Awake()
    {
        idBlocks = new int[x*y];
        node = new Node();
    }

    private void Start()
    {
        texture = Resources.Load<Texture2D>("barley-break");
        sprites = Resources.LoadAll<Sprite>(texture.name);
        Initialization();
        CreateBlocksSequence();
    }

    public void MoveNode(int id)
    {
        Debug.Log("id = " + (id-1));
        Debug.Log("Id = " + idBlocks[id-1]);
    }

    private void CreateBlocksSequence()
    {
        int firstCoordX = -3;
        int firstCoordY = 4;

        int N = firstCoordX+(2*x);
        int M = firstCoordY-(2*y);

        int id = 0;

        for (int j = firstCoordY; j >= M + 2; j-=2)
        {
            for (int i = firstCoordX; i <= N - 2; i+=2)
            {
                Node node = CreateNode(i, j, id);
                CreateObject(node);

                id++;
            }
        }
    }

    private void CreateObject(Node node)
    {
        SpriteRenderer sr;

        GameObject obj = Instantiate(prefabNode, new Vector2(node.X, node.Y), Quaternion.identity);
        sr = obj.GetComponent<SpriteRenderer>();
        int Id = obj.GetComponent<Node>().Id = node.Id;
        obj.GetComponent<Node>().X = node.X;
        obj.GetComponent<Node>().Y = node.Y;
        sr.sprite = sprites[node.Id-1];

        gameObjects[Id-1] = obj;
        idBlocks[Id-1] = Id;
    }

    private Node CreateNode(int x, int y, int id)
    {
        node.X = x;
        node.Y = y;
        node.Id = id+1;

        return node;
    }
}

Консоль

IndexOutOfRangeException: Index was outside the bounds of the array. GameTest.MoveNode (System.Int32 id) (at Assets/Scripts/GameTest.cs:40) Node.OnPointerClick (UnityEngine.EventSystems.PointerEventData pointerEventData) (at Assets/Scripts/Node.cs:26) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at D:/programms/Unity/Hub/Editor/2020.1.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at D:/programms/Unity/Hub/Editor/2020.1.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update() (at D:/programms/Unity/Hub/Editor/2020.1.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:376)

На всякий случай я решил вывести все элементы при запуске программы, все выводится нормально с первого по последний элемент.


Ответы (0 шт):