- Home /
Give prefab a parent
I would like to make an instantiated object the child of the object it spawns on. The object it spawns on is an empty game object.
It keeps giving me an error "setting parent of a transform in a prefab is disabled to prevent data loss"
The ultimate goal is to have the object "polly" spawn on the empty game object as its child. When the empty game object has zero chilidren/poly has been destroyed another will spawn.
Thanks
var polly : GameObject;
function Start () {
var ins : GameObject = Instantiate(polly, transform.position, transform.rotation);
}
function Update () {
if(transform.childCount <= 0){
Debug.Log("apple");
polly.transform.parent = transform;
}
}
Answer by Montraydavis · Oct 31, 2012 at 04:06 AM
The problem is you're not actually assigning polly to the parent. Do it like so
function AttachPolly ( )
{
var polly = Instantiate ( pollyObject, transform.position, transform.rotation ) ;
polly.transform.parent = parentObject ;
}
Great. I am glad that it did. $$anonymous$$ind hitting thumbs up? ;) . Thanks for using Unity3D. It always $$anonymous$$ches me when I help others on here.
Answer by Cosmology27 · Jan 27, 2020 at 03:11 AM
Sorry for the necro, but having this issue, and the code isn't working. I'm guessing "parentObject" is a variable you're assuming I'll predefine prior to that line. When I do this, I create a serialized field where I declare in the inspector what the gameobject will be, however, in this line of code, it's telling me I cannot implicitly convert a gameobject to a transform. In the line below, the "goCreate" is a gameobject I declared, the ShipPos is the position of the ship the laser is coming out of, and laserParent is a gameobject I defined before.
Also, is there a way to just make the object NOT have a parent? Currently my laser is parented to the ship, so when the ship moves, so do its lasers, which is obviously not right, lol.
var laser = Instantiate(goCreate, ShipPos, Quaternion.identity);
laser.transform.parent = laserParent;