instantiating into array

Hello,

Straight to the topic:

Is there any way to instantiate objects straight into the array of game objects ?

using UnityEngine;
using System.Collections;

public class Background : MonoBehaviour {
	public GameObject [][] background;
	public GameObject cube; 
	private float transform_x ;
	private float transform_z ;
	
	// Use this for initialization
	void Start () 
	{
		transform_x = -6f;
		transform_z = -11f;
		for( int x = 0; x < 7; x ++)
		{
			for(int y = 0; y < 12; y ++)
			{
				Instantiate(cube, new Vector3 ( transform_x, 0, transform_z), Quaternion.Euler(90,0,90));

				transform_z =transform_z + 2;
				if(transform_z == 13)
				{
					transform_z = -11;
				}
			}
			transform_x = transform_x +  2;
		}
		
	}
	
	
	
}

I’ve got this so far but i don’t really know how to throw this instantiated game object into the array.

any hints? :F

If I understand your question right, that this might help.

background[x][y] = Instantiate(cube, new Vector3 ( transform_x, 0, transform_z), Quaternion.Euler(90,0,90)) as GameObject;

First you need to create the array (unless it has been created elsewhere).

Then you can set values to it like so array[x][y] = value;

What you have is called a jagged array. They are usually used when each row have different lengths. In your case you could use a two-dimensional array.

// Jagged array
public class Background : MonoBehaviour
{
    public int backgroundWidth = 7;
    public int backgroundHeight = 12;
    public GameObject[][] background;
    public GameObject cube;

    private float transform_x;
    private float transform_z;

    void Start()
    {
        transform_x = -6f;
        transform_z = -11f;

        background = new GameObject[backgroundWidth][];
        for (int x = 0; x < backgroundWidth; x++)
        {
            background[x] = new GameObject[backgroundHeight];
            for (int y = 0; y < backgroundHeight; y++)
            {
                Vector3 position = new Vector3(transform_x, 0, transform_z);
                Quaternion rotation = Quaternion.Euler(90, 0, 90);
                background[x][y] = (GameObject)Instantiate(cube, position, rotation);

                transform_z = transform_z + 2;
                if (transform_z == 13)
                {
                    transform_z = -11;
                }
            }
            transform_x = transform_x + 2;
        }
    }
}

And an example with a 2d array.

// Two-dimensional array
public class Background : MonoBehaviour
{
    public int backgroundWidth = 7;
    public int backgroundHeight = 12;
    public GameObject[,] background;
    public GameObject cube;

    private float transform_x;
    private float transform_z;

    void Start()
    {
        transform_x = -6f;
        transform_z = -11f;

        background = new GameObject[backgroundWidth, backgroundHeight];
        for (int x = 0; x < backgroundWidth; x++)
        {
            for (int y = 0; y < backgroundHeight; y++)
            {
                Vector3 position = new Vector3(transform_x, 0, transform_z);
                Quaternion rotation = Quaternion.Euler(90, 0, 90);
                background[x, y] = (GameObject)Instantiate(cube, position, rotation);

                transform_z = transform_z + 2;
                if (transform_z == 13)
                {
                    transform_z = -11;
                }
            }
            transform_x = transform_x + 2;
        }
    }
}