-1

I'm creating chess in unity, and I want to create the chess board using a 2D array of cells. I was able to create an array of pieces and could reference them with their respective prefabs in Unity. But I have no option to reference the 2D array.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class gameManager : MonoBehaviour
{
    public GameObject[,] cell = new GameObject[7,7];
    public GameObject[] Wpawn = new GameObject[7];
    public GameObject[] Bpawn  = new GameObject[7];
    public GameObject[] Wrook = new GameObject[1];
    public GameObject[] Brook = new GameObject[1];
    public GameObject[] Wknight = new GameObject[1];
    public GameObject[] Bknight = new GameObject[1];
    public GameObject[] Wbishop = new GameObject[1];
    public GameObject[] Bbishop = new GameObject[1];
    public GameObject Wking;
    public GameObject Bking;
    public GameObject Wqueen;
    public GameObject Bqueen;

    bool[] Bpawn2 = new bool[7];
    bool[] Wpawn2 = new bool[7];

    short turn = 0;

    // Start is called before the first frame update
    void Start()
    {
        #region Board
        for (float y = 0F; y <= 7; y+= 1)
        {
            for (float x = 0F; x <= 7F; x+= 1)
            {
                if (y % 2 == 0)
                {
                    if (x % 2 == 0)
                    {
                       cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.gray; 
                    } else if (x % 2 == 1)
                    {
                        cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.white; 
                    }
                
                } else if (y % 2 == 1)
                {
                    if (x % 2 == 0)
                    {
                       cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.white; 
                    } else if (x % 2 == 1)
                    {
                        cell[(int)x,(int)y].GetComponent<SpriteRenderer>().color = Color.gray; 
                    }
                }
                Instantiate(cell[(int)x,(int)y], new Vector3(x - 3.5F, y -3.5F), transform.rotation);
                
            }
        }
        #endregion
    }
}

As you can see I've properly initialized my 2D array. I only showed you the setboard Region because there were no problems in the rest.

Here is the gamemanager script component and what it is showing me.

enter image description here

There is no option to reference the Cell Prefab

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ALK003
  • 123
  • 6
  • Downvoted for not showing proper research effort. Answered as it's a valid question and comes up often. – Harshdeep Singh Jan 09 '21 at 19:01
  • 1
    @HarshdeepSingh I couldn't research it because I didn't know what keywords to use. Everytime I search it up it shows me something that isn't my problem. I didn't know about serializing. Thanks for explaining though. – ALK003 Jan 09 '21 at 19:14

1 Answers1

0

TL;DR

The default Inspector can only show variables that are Serializable by Unity's Serializer, i.e. files which can be written-to and read-from a file by Unity for saving/loading purposes. 2D arrays are not Serializable, thus they're not visible in the Inspector.

Please familiarize yourself with the Serialization rules in Unity. Here's an answer about what Serialization is.

Alternative

Even List<List> or T[][] type 2D arrays are not Serializable. This has to do with limitations regarding generic serialization in Unity. A simple alternative to define a concrete class as follows:

[Serializable]
public class GameObjectList //not a monobehavior
{
    public List<GameObject> gameObjects;
}

using it later on as field

public GameObjectList[] pieces;

This should show up a list of lists in the Inspector, letting you reference your GameObjects. Of course, this method is a bit cumbersome, having to define a new class every time, but it works.

Notes:

A lot of the limitations surrounding generic serialization have been lifted in Unity 2020.2 update, but you still can't serialize 2D arrays/lists directly.

Harshdeep Singh
  • 305
  • 1
  • 4