I’ve made what I think is a lot of progress but I could still use some help figuring out the adjacents (left, right, top, bottom).
Here’s what I think I have so far:
2 scripts One is the Grid and the other is Cell script which goes on each object thats created.
The Cell script: small but gives each object it’s own position and number value
var position : Vector3;
var number : int;
var adjacents : List.<Transform>; //not implemented yet???
The Grid script is resizable (vector3) in the inspector and each object in the grid is named by its location in the hierarchy (0,0,0) which matches the above position and each object in the grid is randomly created. The objects in the grid are created from a prefab which is a simple cube with a child object which is a TextMesh which mathces the above cell script number.
var cellPrefab : Transform;
var size : Vector3;
var grid : Transform[,];//cannot display 2d arrays
function Start ()
{
CreateGrid();
SetRandomNumbers();
}
function CreateGrid()
{
grid = new Transform[size.x, size.z];
for(var x = 0; x < size.x; x++)
{
for(var z = 0; z < size.z; z++)
{
//We create a new Transform to manipulate later.
var newCell : Transform;
newCell = Instantiate(cellPrefab, new Vector3(x, 0, z), Quaternion.identity);
newCell.name = String.Format("({0},0,{1})",x,z);
newCell.parent = transform;
//this puts the position of itself into the var position of cellscript
newCell.GetComponent(CellScript).position = new Vector3(x, 0, z);
grid[x,z] = newCell;
}
}
}
function SetRandomNumbers()
{
for (var child : Transform in transform)
{
var number = Random.Range(0,10);
//assign the random number to the textMesh
child.GetComponentInChildren(TextMesh).text = number.ToString();
//assign the random number it is into the cell script
child.GetComponent(CellScript).number = number;
}
}
I don’t understand how to get the Neighbors but there is a var for it called “adjacents” in the Cell script which is a List. If you could help that would be amazing.
Also if I were creating a game like Bubble Shooter and shot a cube or sphere at the Grid of Objects how would check each time if they were the same number? (I can change my random number to (0,3)).