Array of Prefabs doesn't get Serialized by Unity

I made a script to easily design my level based on a grid.
it contains a GameObject[,] variable, that gets filled with prefabs, selected form my projectfolders, (not from the scene). based on that he instantiates that part of te level based on when nececary.

My problem is that when my script gets serialized by unity, that array isn’t stored, when it’s loaded again it appears to have a null-value.

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

public class Grid : MonoBehaviour {
  [SerializeField]
  public GameObject[,] grid ;
  public List<Coords> selected = new List<Coords>();
  public static float scale = 1;

  private GameObject[,] ObjectGrid  {
    get
    {
      if (grid == null)
      {
        grid = new GameObject[32, 32];
        Debug.Log("nooooo");
      }
      return grid;
    }
    set {
      grid = value;
    }
  }

  public int Width {
    get
    {
      return ObjectGrid.GetLength(0);
    }
    set {
      GameObject[,] newGrid = new GameObject[value, Height];

      for (int x = 0; x < value && x <  Width; x++)
      {
        for (int y = 0; y < Height; y++)
        {
          newGrid[x, y] = ObjectGrid[x, y];
        }
      }
      ObjectGrid = newGrid;
    }
  }
  public int Height
  {
    get
    {
      return ObjectGrid.GetLength(1);
    }
    set {
      GameObject[,] newGrid = new GameObject[Width, value];

      for (int x = 0; x < Width; x++)
      {
        for (int y = 0; y < value && y < Height; y++)
        {
          newGrid[x, y] = ObjectGrid[x, y];
        }
      }
      ObjectGrid = newGrid;
    }
  }
  public void SetObject(int x, int y, GameObject go)
  {
    ObjectGrid[x, y] = go;
  }
  public void BuildTiles()
  {
    while (transform.childCount > 0)
    {
      DestroyImmediate(transform.GetChild(0).gameObject);
    }
    for (int x = 0; x < Width; x++)
    {
      for (int y = 0; y < Height; y++)
      {
        if (ObjectGrid[x, y] != null)
        {
          GameObject go = Instantiate(ObjectGrid[x, y]);
          go.transform.SetParent(transform);
          go.transform.localPosition = new Vector3((x + 0.5f) * scale, -(y + 0.5f) * scale, 0);
        }
      }
    }
  }

  public void SetObject(Coords c, GameObject go) {
    SetObject(c.x, c.y, go);
  }
  public float MaxX {
    get { return Width * scale; } 
  }
  public float MaxY
  {
    get { return Height * scale; }
  }
  public void UnSelect() {
    selected = new List<Coords>();
  }
  public void AddSelected(Coords item) {
    for (int i = 0; i < selected.Count; i++)
    {
      if (selected_.x == item.x && selected*.y == item.y) return;*_

}
selected.Add(item);
}

public void SetObjectForSelected(GameObject go) {
if (go == null) return;
for (int i = 0; i < selected.Count; i++)
{
SetObject(selected*, go);*
}

BuildTiles();
}

void OnDrawGizmosSelected()
{
int gizmoHeight = 20;
Color linesColor = new Color(0, 0, 0.2f,1);
Color selectedColor = new Color(0, 1, 0, 0.3f);

Gizmos.color = linesColor;
for (int x = 0; x <= Width; x++)
{
Gizmos.DrawLine(new Vector3(transform.position.x + x* scale, transform.position.y, transform.position.z - gizmoHeight), new Vector3(transform.position.x + x* scale, transform.position.y - MaxY, transform.position.z - gizmoHeight));
}
for (int y = 0; y <= Height; y++)
{
Gizmos.DrawLine(new Vector3(transform.position.x, transform.position.y - y * scale, transform.position.z - gizmoHeight), new Vector3(transform.position.x + MaxX, transform.position.y - y * scale, transform.position.z - gizmoHeight));
}

Gizmos.color = selectedColor;
for (int i = 0; i < selected.Count; i++)
{
Gizmos.DrawCube(new Vector3(transform.position.x + (selected.x+ 0.5f)* scale , transform.position.y -(selected_.y + 0.5f) * scale, transform.position.z - gizmoHeight), new Vector3(scale, scale, 0));
}
}_

void Start() {
BuildTiles();
}
public struct Coords {
public int x;
public int y;
}
}

note: I use a custom inspector script to assign the prefabs. But i don’t think that has anything to do with the problem/solution. I can provide it if asked for :slight_smile:

Unfortunately UnityEngine.Objects are not serializable (including GameObjects). If you want to save your GameObjects you have to save the data inside them to a custom class and then reconstruct them from that data.