Assigning a gameobject to an array

I am new to Unity, and programming, and have been searching and reading about arrays but cannot seem to find an answer to my questions.

For me to better understand an array. I thought a simple TicTacToe game would help. So far I have created only a single dimension with 3 cubes that are under an emptyObject as children and taged as “Player”. Obviously their will be a total of 9 in a 2 dimension array. The human players will use the mouse to click on a cube to change the texture to X or O.

Now for the question: How do you assign the cubes name cube1, cube2, cube3 (which is what they are called in hierarchy) to the array.
This is what I have so far that works but does not really identify which cube is which?

void blockArray()
	{
		cubeArray = new string[3];
		cubeArray[0] = GameObject.FindWithTag("Player").tag;
		cubeArray[1] = GameObject.FindWithTag("Player").tag;
		cubeArray[2] = GameObject.FindWithTag("Player").tag;
		print (cubeArray[0]);
		print (cubeArray[1]);
		print (cubeArray[2]);
	}

the outcome of the print statement is just player 3 times. Instead of cube1, cube2, cube3.

An alternative route you can experiment with would be to use a handle to each cube object. You could declare a public list of GameObjects within your class scope, attach the script to your emptyObject, and fill that list with your cubes inside of the inspector.

// Add your cubes into this list from the Unity Inspector.
public List<GameObject> Cubes;

This way you can index into the list and find the cube you want. Ex:

print (Cubes[0].name); // prints: cube1

Well, you find three GameObjects with the tag “Player”, and get their tag, which is “Player”, and assign it to the array elements. Hence it prints out “Player”.

As Key_Less pointed out, you could use the .name property of the game objects, which is what is shown in the hierarchy.

If at all possible, it is probably easier if you assign the cubes to the array in the editor instead of at runtime. Sometimes, you need to dynamically find and assign GameObjects at runtime, but it is more error-prone. I’d suggest assigning the GameObjects in the Unity inspector. You could do that like this:

public class Board : Monobehaviour
{
    //Shows up in the unity inspector.
    public GameObject[] cubes;

    private void Awake()
    {
        //TODO: Do stuff with cubes.
    }
}

And then assign the cube GameObjects in the editor at design time.

If you really want to do populate the array at runtime, and you have uniquely named your cubes, you could also do something like this:

private void BlockArray()
{
    GameObject parentGO = GameObject.FindWithTag("Player");
    cubeArray = new GameObject[parentGO.transform.childCount];
    for(int i = 0; i < parentGO.transform.childCount; ++i)
    {
        cubeArray *= GameObject.Find("cube" + i);*

}
}
Personally, I don’t really like using methods like FindWithTag or Find if possible, since they introduce assumptions about scene structure and could compile fine but blow up at runtime (what if the tag changes? or what if the someone changes the cube names?).

try using FindGameObjectsWithTag instead, since it return all the objects with the player tag, not just the first one.

void blockArray()
        {
           cubeArray = new string[3];
           cubeArray[0] = GameObject.FindGameObjectsWithTag("Player")[0].name;
           cubeArray[1] = GameObject.FindGameObjectsWithTag("Player")[1].name;
           cubeArray[2] = GameObject.FindGameObjectsWithTag("Player")[2].name;
           print (cubeArray[0]);
           print (cubeArray[1]);
           print (cubeArray[2]);
        }