unity declare gameobject array code example

Example 1: unity array of gameobjects

If you want to create an array of this gameobject, you can do this as follows:

int Size = 10;     //Number of objects
GameObject[] Tiles = new GameObject[Size];
Let's assume you have created a prefab, called "Tile". If we want to initialize the array's gameobjects, we could do it like this:

//Loop for the entire size of the array, 10 in this case
for (int i = 0; i < Size; i++)
{
    //Create the game object
    Tiles[i] = GameObject.Instantiate (Resources.Load ("Tile")) as GameObject;  

    //Position it in the scene
    Tiles[i].transform.position = new Vector3(i * 0.32f, 0, 0);
}
If you assign the following script to the Start() function of a gameobject in your scene, you will see 10 Tile prefab objects appear when you Run your game.

Example 2: how to make an array of arrays unity

//creates an array of arrays int[][] myarray;  //creates an array which contains 20 arrays the arrays are all empty and unitialized, they will need to be initialized, there is currently just 20 starting points reserved in memory. They point to nothing really. myarrays = new int[20][];  //the first array is size 3 with the element 0 & 1 & 1 myarrays[0] = {0,1,1};  myarrays[0][0] = ? //based on the statement above it equals 0 myarrays[0][1] = ? //based on the statement above it equals 1 myarrays[0][2] = ? //based on the statement above it equals 1 as well   //the following code will take every array in myarrays and make them equal to arrays of size 10, the arrays are empty but now they point to something, its probably zero by default due to internal code. for (int x = 0; x <= myarrays.size(); x++) {     myarrays[x] = new int[10]; } //technically the code above still hasnt set what the value of each element in the arrays are, it simply now says its size is definitely 10, internally the code has probably set those memory values to zero however to help prevent unexpected errors, that's not guaranteed I don't think however and it would probably be best to set each value to be certain different machines and things dont do things differently and create unexpected and hard to find errors.   //the follow code will create an array that contains 20 arrays, each of those arrays are of size 10, and each of them will have the value set to 0;  int[][] myarrays = new int[20][];  //careful on this for statement, it is less than not less than or equal too, this has to do with size starting to count from 1 and x starting to count from 0. You must start from 0 because the first index in an array is 0.  for(int x = 0; x < myarrays.size(); x++) {       myarrays[x] = new int[10];    for (int y = 0; y < myarrays[x].size(); y++)     {        myarrays[x][y] = 0;     } }  //Do note that i used the size function to get the size, thats because you shouldn't used numbers like 20 or 10 even if thats what you think you want. You should declare a constant called NUMBER_OF_ARRAYS and NUMBER_OF_ELEMENTS_IN_ARRAYS or something like that and substitute those that way you can change the size of the arrays with a few keyboard strokes.

Tags:

Misc Example