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);
}
}