script add prefab instances to empty gameobject

I want to add multiple instances of a prefab to my scene on start and make them all children of an empty GameObject. This is what I have:

public class LightWave01 : MonoBehaviour {

    public Transform light_fixture;
    public int num_x;
    public int num_z;
    public float space_x;
    public float space_z;
    public float freq1;
    public float freq2;
    public float amp;
    public float start_pt_x;
    public float start_pt_y;
    public float start_pt_z;

    private GameObject light_group;

    // Use this for initialization
    void Start()
    {
        light_group = new GameObject("LightGroup");

        for (int j = 0; j < num_z; j++)
        {
            for (int i = 0; i < num_x; i++)
            {
                float x = -1.0f * (i * space_x - start_pt_x);
                float z = -1.0f * (j * space_z - start_pt_y);

                double f1 = Math.Sin(freq1 * x * Math.PI / num_x);
                double f2 = Math.Sin(freq2 * z * Math.PI / num_z);
                double y = amp * (f1 * f2) + 0.02 * j + start_pt_z;

                float floatY = (float)y;

                GameObject l = (GameObject) Instantiate(light_fixture, new Vector3(x, floatY, z), Quaternion.identity);
                l.transform.parent = light_group.transform;
            }
        }
    }
}

It creates a GameObject called “LightGroup,” but only adds one instance of the “light_fixture” prefab, which is added to the bottom of the Hierarchy, not to the LightGroup GameObject. What am I missing?

I don’t see any specific error as long as “light_fixture” is of type “GameObject”. Are you sure you don’t get any error in the console? Tried adding a Debug.Log() into the inner for loop and print the values for “j” and “i”?

If the parenting doesn’t work but the instantiate does work it’s most likely because the cast to GameObject (in line 21) has failed because your prefab variable “light_fixture” doesn’t reference a GameObject. Since the declaration of the variable is missing we can’t tell for sure. In any case you should get an Exception about that.

Another thing which i could imagine is that there’s an error in a script which is attached to the prefab you’re instantiating. If Awake or OnEnable throw an exception that might fall through to your Instantiate line. However in that case you also should get an Exception in the console ^^.

edit
Well, as i though the type of your prefab and the type you’re trying to cast to doesn’t match. Your “light_fixture” variable is of type “Transform”. When you drag a gameobject to that variable you don’t store a reference to the gameobject of the prefab, but to it’s Transform component.

Instantiate will return the same type that you pass to it. If you pass a component (like Transform but any Component is possible as well) to Instantiate it will clone the whole gameobject that contains that component. However the returned object will be the component you passed in.

So in your case Instantiate will return the Transform component of the cloned prefab. Of course you can’t cast a reference to a Transform into a reference to a GameObject.

Change your code to:

Transform l = (Transform) Instantiate(light_fixture, new Vector3(x, floatY, z), Quaternion.identity);
l.parent = light_group.transform;