Deleted object tagged as missing instead of null.

Hello, I have a smaller question.


I have a cript that creates a grid made out of cubes.After the grid is created the game than randomly chooses some objects from a list and deletes them.


My problem is when I delete this object, the reference in the editor marks the object as missing.
I understand that the object has reference to the neighbour but when it gets deleted the object canˇt find the its neighbour.
My question is how could I avoid this or repair it ?

Here is my script.


Map generator
public class MapGenerator : MonoBehaviour
{
public int mapSize;
public int numberOfWaterHoles;

    public CubeCell cubeCell;
    public List<CubeCell> cubeCells = new List<CubeCell>();

    public bool landIsGenerated;
    public int i;
    public bool generateLand;
    void Start()
    {
        
        GenerateMap();
    }

    void GenerateMap() 
    {
        for (int x = 0 ; x < mapSize; x++)
        {
            for (int z = 0; z < mapSize; z++)
            {
                CreateTile(x, z, i++);
            }
        }

        if (i == mapSize * mapSize) { landIsGenerated = true; } else { landIsGenerated = false; }

        if (landIsGenerated) 
        { 
            CreateWater();
        }
    }

    void CreateTile(int x,int z,int i) 
    {
        Vector3 tilePosition;
        tilePosition.x = x * 1f;
        tilePosition.y = 0;
        tilePosition.z = z * 1f;

        CubeCell cubeTile = Instantiate<CubeCell>(cubeCell);
  
        cubeCells.Add(cubeTile);
        cubeTile.transform.SetParent(transform, false);
        cubeTile.transform.localPosition = tilePosition;

        if (z > 0) { CubeCell.GetNeighbours_EastAndWest(cubeTile, cubeCells[i - 1]); }
        if (x > 0) { CubeCell.GetNeighbours_NorthAndSouth(cubeTile, cubeCells[i - mapSize]); }
        
    }
  void CreateWater() 
    {
        //Choose random number from a list delete the gameobject,empty space will be used to simulate water.Remaining objects will be used as land.
        
        for (int o = 0; o < numberOfWaterHoles; o++)
        {

            int randomNumber = Random.Range(o, (mapSize*mapSize));

            var randomObject = new CubeCell[numberOfWaterHoles];
            randomObject[o] = cubeCells[randomNumber];

            cubeCells[randomNumber] = cubeCells[o];
            cubeCells[o] = randomObject[o];
            cubeCells[o].DestroyObject(); 
        }        
    }    
}

Cell script

public class CubeCell : MonoBehaviour
{
    [Header("Neighbours")]
    public CubeCell east, north, west, south;

    [Header("Land types")]
    public GameObject Grass_North;
    public GameObject Grass_South;
    public GameObject Grass_East;
    public GameObject Grass_West;
    public GameObject Grass_NorthEast;
    public GameObject Grass_NorthWest;
    public GameObject Grass_SouthEast;
    public GameObject Grass_SouthWest;
    public GameObject Grass_Middle;

    public GameObject currentLand;


    private void Start()
    {

            CreateLand();
      
           
    }
    public static void GetNeighbours_NorthAndSouth(CubeCell north, CubeCell south) 
    {
        north.west = south;
        south.east = north;
    }

    public static void GetNeighbours_EastAndWest(CubeCell east, CubeCell west)
    {
        east.south = west;
        west.north = east;
    }

    void CreateLand() 
    {
        Vector3 position = new Vector3(0,0,0);

        if(north == null && east == null) { currentLand = Grass_NorthEast; } else { currentLand = Grass_Middle; }
        GameObject newLand = Instantiate<GameObject>(currentLand);
        newLand.transform.localPosition = position;
        newLand.transform.SetParent(transform, false);
    }

    public void DestroyObject() 
    {
        GameObject.Destroy(gameObject);
    }
}

You could remove the object you wish to delete from the list or remove or set to null any references other cells have first and then destroy the object? If an object gets destroyed, let the neighbours know that the object will be gone. Then you could destroy the object.

something like:

public void DestroyObject() 
     {
         //go to all neighbours of this cell.
         //set the neighbours reference of this cell to null like: 'Tell neighbour east that object west is null'
         //also remove this cell from any list you might have for easy reference.

         //bombs away!
         GameObject.Destroy(gameObject);
     }

I hope this can be helpful. If not let me know.