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
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.
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
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…
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.