2D Grid, 3d objects

Is there any chance someone could explain how to access objects of a grid.

I filled my first grid with gameObjects, how would I access those gameObjects? In other words how would I ask or check if there were 3 cubes right next to each other, (in any row or column)? Or starting out it seems like I have to find the neighbors.

Updated Code and Post BelowPlease Keep Reading.

Maybe this will help,
http://forum.unity3d.com/threads/144886-Grid-Framework-scripting-and-editor-plugins

I appreciate the link but I’d like to learn or see how’s it’s done before going the plugin route.

In your posted example I don’t see you use an array or other data container to hold the grid. Usually you’ll have a single or double dimension array to store the references to the GameObject that are currently occupying grid cell.

public my2DGrid : GameObject[,]=new GameObject[5,5];

To use the grid you would need a few support functions:

Translate real world position to the grid cell position - in a simple system you can project onto a plane by ignoring the Y component of an objects position and scaling the X and Z values to find the proper grid cell.

Translate grid position to real world position - simple scaling of an Objects X and Z values based on grid position where the Objects Y component is arbitrarily set to a value that makes sense in your game.

Get neighbours method that returns a list / array of neighbouring grid cell IDs - this usually involves finding the grid offsets for surrounding cells based on the grid cell index of the start cell. Something like startCellX+1, startCellX-1, startCellY+1, startCellY-1. This is then used to answer questions like “Is the next cell to my left relative to the grid currently occupied?”

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

Check this astar pathfinding example. There you will see how to get neighbour cells of the grid.
http://forum.unity3d.com/threads/92359-3d-Astar-pathfinding

I downloaded the project file from your post which has the Find Neighbors in the World Script but its a lot to take in because Im not sure whats really happening in the script. Which I do appreciate Im just not that advanced yet (not knowing what all the nodes, breadcrumbs and everything else is?