- Home /
inserting prefabs into GameObject
hi, i would to ask, is there any possible to add prefabs into empty game objects programmatically( in run time)
i generated the prefabs like this
var object : Transform[];
function Start(){
Spawn();
}
function Spawn(){
while(true){
yield WaitForSeconds(spawnTime.targettingFrequency);
var position = Vector3(Random.Range(-13, 13), 40, 100);
shuffle.add(0,5);
shuffle.add(1,1);
shuffle.add(2,2);
Instantiate (object[shuffle.next()], position, Quaternion.identity);
}
}
every object i created i want to add to empty... any help?
I believe you mean gameobjects in to prefabs... As a prefab holds a gameobject...? Or am I wrong?
Also, if I'm right, you can just make all the prefabs, have an array, change the size, and then add all the prefabs in the editor
actually i want to add the prefabs into gameObject while run. it is possible?
Answer by uhahaha · Dec 16, 2010 at 08:39 AM
Is this what you want?
var aPrefab: GameObject;
var emptyGO: GameObject;
function Start ()
{
emptyGO = Instantiate(aPrefab, emptyGO.transform.position, emptyGO.transform.rotation);
}
Answer by Lab013 · Mar 17, 2011 at 03:30 AM
Or perhaps you want to set transform.parent?
Based on my understanding of what you were trying to do, this is the one you want. I have just made use of it for an RTS game I am making. When building a new unit from a given building I Instantiate a prefab for the unit and then make the player the parent of the prefab.
Answer by Maulik2208 · Dec 10, 2012 at 10:09 AM
a snap or diagram or some more info about your question will help to better understand so please be specific and provide something which clearly explain the stuff
Your answer
![](https://koobas.hobune.stream/wayback/20220613091221im_/https://answers.unity.com/themes/thub/images/avi.jpg)