Need some help with the graphics part of my game.

Hello,

I’m making a 3D clone of Conway’s Game Of Life. My logic works fine, but my graphics are a bit heavy on the system.

Currently I’m instantiating new cubes each Update(). This consumes time of course and I’m looking for an alternative. Something I had in mind was instantiating all the cubes in the first frame and then change it’s shader to show it or not.

This is where I run into trouble.

I instantiate the cubes as follows:

for (int x = 0; x < gridX - 1; x++)
		{
			for (int y = 0; y < gridY - 1; y++)
			{
				Instantiate(cubePrefab, new Vector3(x + 0.4f * x, 1f, y + 0.4f * y), Quaternion.identity);
			}
		}

But now I have no reference to these cubes, other then its tag.

The array that holds the info about my cubes is this:

public struct CubeStruct
	{
		public Transform cube;     //this is where I would want to save my cube in
		public bool alive;              //Indicates whether this cell is alive or not
		public Color color;            //Here I want to save the color, not of importance here.
	}
	
	public CubeStruct[,] cubeArray = new CubeStruct[gridX, gridY];

How can I link these two together, so my array has a reference to the cube?

After some more pondering I might have found something. No time to implement it yet, so I’ll just post it here, I might get some responses meanwhile :slight_smile:

There’s a main script that will calculate the arrays and every cube will have a little script to handle its own shader.
When I’m instantiating my cubes I could broadcast a message to my cubes. This message contains an ID so that I can track them. The cube itself will see if it has an ID, if not it will take the one generated after it has been created.

But that brings me to another problem. These scripts aren’t executed before my main script has finished.

You could add the GameObject instance to your CubeStruct, so you can link the GameObject with the others params/variables.

To avoid instanciating of Cubes a on a per frame basis, you could :
-instantiate a pool of several cubes (10, 100 ?, 1000 ?)
-unactivate them (active = false)
-store them in an ArrayList
(you can use a static C# class for that)

when you have to “create” a cube, you :
-get the first cube from you arraylist (if any left)
-remove from array list
-activate it : active = true

when you have to “destroy” a cube, you :
-unactivate it
-add to array list

Do you have any idea how I link them when I instantiate() them? Because Instantiate() doesn’t have an ID parameter or anything else with which I can identify the single cubes. That’s really the only thing I need to make a work, some sort of ID.

Looks like I didn’t do my research well :wink:

I can instantiate now, but it gives NullReferenceExceptions when I try to set active = false.

This is the instantiate and active = false code:

#region instantiate all cubes
		for (int x = 1; x < gridX - 2; x++)
		{
			for (int y = 1; y < gridY - 2; y++)
			{
				grid[x, y].cube = Instantiate(cubePrefab, new Vector3(x + 0.4f * x, 1f, y + 0.4f * y), Quaternion.identity) as GameObject;		
				grid[x, y].cube.active = false; //this line gives the NullReferenceExceptions.
			}
		}
#endregion

I’ve been analyzing what happens, and it seems that it can’t instantiate the cubes in the grid[,].cube variable

EDIT: Found it, but now I can’t set inactive objects to active, so I think I’m just going to change its alpha, based on whether it’s active or not.

Yet another EDIT: getting quite desperate now. It keeps saying there is no reference to the object, but there should be one.

This is the buggy code:

void DrawGeneration()
{
	for (int x = 1; x < gridX - 2; x++)
	{
		for (int y = 1; y < gridY - 2; y++)
		{
			if (grid[x, y].alive == true)
			{
				grid[x, y].cube.renderer.enabled = true;
			}
			else
			{
				grid[x, y].cube.renderer.enabled = false;
			}
		}
	}
}

Both lines give me this error:

My initialisation code:

for (int x = 1; x < gridX - 2; x++)
{
	for (int y = 1; y < gridY - 2; y++)
	{
		grid[x, y].cube = Instantiate(cubePrefab, new Vector3(x + 0.4f * x, 1f, y + 0.4f * y), Quaternion.identity) as Transform;		
		grid[x, y].cube.renderer.enabled = false;
	}
}

The first generation works like a charm, but then it starts generating errors.

did you properly construct your objects stored in grid[ ][ ] array ?

Maybe you forgot this line :
grid[x, y] = new CubeStruct();
(before using you grid[x,y] object you need to create it)

Didn’t manage to answer yesterday, some issue with tokens on the forum.
But I have that piece of code correct. I really don’t know what could’ve gone wrong…

try to break the NullReferenceException lines into several calls, to get to know who really is null.
Is this the cube ? Is this the renderer ?

I managed to find it, thanks!

I had 2 arrays, and I was replacing the old one with the new one. But I replaced the whole array, instead of just the grid[,].alive variable. The new array didn’t contain the instantiated cubes of course.