- Home /
Instantiate array of prefabs
Hi,
I have created 3 prefabs which are platforms vary in size, position and rotation. Now i want to instantiate the prefabs in such a way that one prefab should appear at the end of another prefab. Presently i have created prefab and parented to the empty GameObject with (0,0,0) position. Please help...
If you don't know the size of the prefabs, you can, for example, access their $$anonymous$$eshFilter component, get the mesh, get it's Bounds, and use the $$anonymous$$ and max values of the Bounds to find out how far away the next prefab needs to be instantiated at.
Answer by Stensvad · Jun 11, 2015 at 09:05 AM
You could reference each of the objects positions and instantiate them based on the one that was instantiated before it. public GameObject prefab1; public GameObject prefab2; public GameObject prefab3;
public transform transformPrefab1;
public transform transformPrefab2;
public transform transformPrefab3;
offset = new Vector3(transformPrefab1.x - 10, transformPrefab1.y - 10, transformPrefab1.z -10);
//once you instantiate your first object, you should be able to reference the others with an offset
instantiate(prefab2, transformPrefab1.position - offset, transformPrefab1.rotation);
this is untested, but the general idea of what I would do. But I don't know what exactly you are trying to accomplish. Hope it helps.