Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

script add prefab instances to empty gameobject

$
0
0
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?

Viewing all articles
Browse latest Browse all 302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>