3D Cube grid data save

I have drawn a 3D 8x8x8 grid and stored a class containing a cube object and other data into an array. I can select each one independently using mouse, change its colour etc, however I eventually want to load and save a data such as a whether it is selected and the colour of each cube to a file.

This is the first time I have used unity as well as JavaScript, I usually use C++ or Csharp. It seems that I can only have 1 ‘Tag’ for each game object, which is useless for this problem. I have experimented with multidimensional arrays in Javascript etc also, but doesn’t seem suitable.

Is there a good way to attach additional variables to each of the cubes and quickly search for the selected cube in an array? The relationships between game objects and scripts are starting to confuse me.

//Draw cubes and store data
function Awake () {
for(var z=0;z<8;z++)
{
	for(var y=0;y<8;y++)
	{
		for(var x=0;x<8;x++)
		{
		
			Instantiate(newCube,position,rotation); 	
			newCube.position = position;
			cubeList[cubeCount].Cube = newCube;		
			
			cubeCount++;
			position.x += 1.1;
		}
		position.x = 0;
		position.y += 1.1;
	}
	position.y = 0;
	position.z += 1.1;
}
}

At the moment to find the cube in the array I am using:

function OnMouseDown()
{

for(i=0;i<511;i++)
{
	
	if(transform.position.x == Main.cubeList*.xpos)*
  • {*
    _ Main.cubeList*.colour = currentColour;_
    _Main.cubeList.selected = isSelected;*_

* }*
}
}

You know you can use C# with Unity if you feel more comfortable with it.

There’s probably a few ways to do this, these come to mind:

  1. You can make a script (class) and attach that to a cube, then make a prefab of the cube. Then instantiate that prefab-cube 8x8x8 times, each will have that script/class with your variables. You can then iterate over all those gameobjects, getting the Component (your script). Something like GetComponentsInChildren() assuming you parent all the cubes under some gameobject to group them. Anyway, look at each instance’s component of your class to compare the variables you care about.

  2. Name each instance differently, including handy data. Ex: ID-1_Group-3_Owner-Bill or whatever. Then you can construct the name programatically and use GameObject.Find to find it. Or better to keep a list of gameobjects and just check the .name

    var newGO : GameObject = Instantiate (blah blah blah

    newGo.name = “ID-” + i + “_Group-” + g + “_Owner-”+ o;

  3. Keep that data separate, in its own data structure, handle as you normally would if this was not a graphics app, but one of the data would be a GameObject, and would refer to a particular cube’s gameobject. Then you can search this data structure any way you like, and that game object would just be attached to it. Pretty much what you have done.