- Home /
Instantiate object as a child of other instantiate
GameObject p = Instantiate(build, new Vector3(posx,posy+0.525f,posz+0.6f),Quaternion.identity) as GameObject;
GameObject go = Instantiate(Cube_Transform, new Vector3(posx,posy,posz), Quaternion.identity) as GameObject;
How to instantiate object go as a child of object p? Both of them are not present at the scene at once, also they appear through this script at once
You have to use Google for this. There are many posts of similar topic.
Don't know why my answer wasn't posted, so I can't even say enough "good" words to express my opinion. I tried the same line as in these suggested answers almost a week ago (and yes - I used Google) and it gave me an error NullReferenceException: Object reference not set to an instance of an object in line
go.transform.parent = p.transform;
and that's why I asked this question here (and yes - I watched answers in Similar topics). And? Advice to use Google and the same line, that gives an error? Or just please say(somebody) what to do with the error
Answer by robertbu · May 19, 2014 at 04:16 AM
GameObject p = Instantiate(build, new Vector3(posx,posy+0.525f,posz+0.6f),Quaternion.identity) as GameObject;
GameObject go = Instantiate(Cube_Transform, new Vector3(posx,posy,posz), Quaternion.identity) as GameObject;
go.transform.parent = p.transform;
NullReferenceException: Object reference not set to an instance of an object in line
go.transform.parent = p.transform;
I'm guessing that 'Cube_Transform' is a transform not a game object. If so you either need to make 'Cube_Transform a game object, or you need do something like:
Transform tr = Instantiate(Cube_Transform, new Vector3(posx,posy,posz), Quaternion.identity) as Transform;
The if 'build' is a game object, you would do:
tr.parent = p.transform;
Or if 'build' is a Transform, you need to make the appropriate changes in the Instantiate line as above, and do:
tr.parent = p;
Answer by xt-xylophone · May 19, 2014 at 04:16 AM
An object's transform has a 'parent' variable. Simply go:
go.transform.parent = p.transform;
Try googling or searching the documentation(which is AMAZING) next time.
Your answer