// Generate tyhe grid cubes void GenerateGridCubes() { // Initialise the grid cube array gridCubeArray = new GameObject[gridWidth, gridHeight]; // Build the map grid - loop through height of grid for (int z = 0; z < gridHeight; z++) { // Loop through width of grid for (int x = 0; x < gridWidth; x++) { // Build the grid - instantiate grid cube calculated position - assign the cube as a child of the gridCubes object GameObject cube = Instantiate(gridCube, new Vector3(x * 1.0f, 0.0f, z * 1.0f), Quaternion.identity, gridCubes.transform) as GameObject; cube.transform.localScale = new Vector3(cubeScale, cubeScale, cubeScale); // Assign the grid cube to the array gridCubeArray[x, z] = cube; // Reference to the cube data CubeData cubeData = gridCubeArray[x, z].GetComponent <CubeData>(); // Set the cubes coord in the grid and set all cubes to closed (initially) cubeData.SetCubeCoord(new Vector3(x, 0.0f, z)); cubeData.SetCubeOpen(false); // Random between 0 and 1 - used to select rock type float crystalChance = Random.Range(0.0f, 1.0f); // Set the grid borders as border cubes - Set the gird cube indestructible and the cube type as border if (x == 0 || z == 0 || x == 1 || z == 1 || x == gridWidth - 1 || z == gridHeight - 1 || x == gridWidth - 2 || z == gridHeight - 2) { cubeData.SetAsBorderCube(); } // Set the cube as crystal cubes - Set the gird cube destructible and the cube type as rock else if (crystalChance < crystalCubeChance) { cubeData.SetAsCystalCube(); } // Set the rest of the cubes as rock cubes -- Set the gird cube destructible and the cube type as rock else { cubeData.SetAsRockCube(); } // If the cube is a rock/crystal or border cube if (cubeData.GetCubeType() == CUBETYPE.BORDER || cubeData.GetCubeType() == CUBETYPE.ROCK || cubeData.GetCubeType() == CUBETYPE.CRYSTAL) { // Set the scale and rotation of the grid cubes cube.transform.localScale = new Vector3(cubeScale, cubeScale, cubeScale); cube.transform.rotation = Random.rotation; } } } Debug.Log("MAP - GEN GRID CUBES - COMPLETE"); }