Skip to content

Conway’s Game Of Life in 3D

Game of Life Setup

Go ahead and delete the previously created Cube object from the life scene (the cube prefab will continue to exist in Prefab form under the prefab directory).

Now, let's create a grid of cubes programmatically. Create a new Empty Gameobject, call it Grid and attach a new script also called Grid to it:

Open up the script and add the following skeleton code:

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

public class Grid : MonoBehaviour 
{
    void Start() 
    {
        initialSetup();
        displayMatrix();
    }

    private void initialSetup()
    {

    }

    private void displayMatrix()
    {

    }
}

Obviously, the script above doesn't do much yet. Let's create and initialize a 3 dimensional array ob GameObject's:

    public GameObject cube;

    public int xCubes;
    public int yCubes;
    public int zCubes;

    ...

    private GameObject[,,] matrix;

    ...

    private void initialSetup()
    {
        matrix = new GameObject[xCubes, yCubes, zCubes];

        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                for (int k = 0; k < matrix.GetLength(2); k++)
                {
                    GameObject cubeClone = Instantiate(
                        cube, 
                        new Vector3(i, j, k), 
                        Quaternion.identity
                    );
                }
            }
        }
    }
}

Back in the Unity Editor, assign the Cube prefab to the Grid script and set the following values values:

Note that the z Cubes variable is currently set to 1: let's keep it 2D for the time being.

If you run the project, you will see something similar to this:

The reason you are only seeing "one" cube is because they are aligned perfectly next to each other. Let's fix this by adding a bit of padding to each cube:

    ...

    private float scale = 0.9f;
    private Vector3 scaleVector;

    private void initialSetup()
    {
        ...
        scaleVector = new Vector3(scale, scale, scale);
        ...
    }

    private void initialSetup()
    {
        matrix = new GameObject[xCubes, yCubes, zCubes];
        scaleVector = new Vector3(scale, scale, scale);

        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                for (int k = 0; k < matrix.GetLength(2); k++)
                {
                    GameObject cubeClone = Instantiate(
                        cube, 
                        new Vector3(i, j, k), 
                        Quaternion.identity
                    );

                    cubeClone.transform.localScale = scaleVector;

                    matrix[i, j, k] = cubeClone;
                }
            }
        }
    }
}

which now results in properly displaying the different cubes:

Alright, we got our basic grid setup. In the next chapter, we will add Conway's rules and a way to have our cubes evolve based on the rules.

1 thought on “Conway’s Game Of Life in 3D

  1. telepathy

    Heya i'm foг the first time here. I found this board and Ӏ find It reallү useful & it heⅼped me out muсh.
    I hope to givе something back and aid others like you aided me.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *