- Home /
Change position of instantiated parent's children
I have alot of gameobjects that are all children of an empty gameobject, and after colliding with a trigger, I need to instantiate all of those gameobjects and adjust their position. If I instantiate the parent object, as according to the scripting api, the children will take the position of the original object instead of where i position the new parent. How do I fix this? This is my current line of code
Instantiate(spawn, new Vector3(0, 0, currentZ + 103), Quaternion.identity);
but since I am instantiating a parent object that is empty, all of the children do not take the new position. Any help is greatly appreciated.
Answer by toddisarockstar · Jun 08, 2017 at 02:50 AM
No actually.
when instantiating a gameobject, the fact that it is an "empty" is irrelevant. (empty game objects still have a transform). so if you instantiate an empty gameobject with children attached, The children will spawn with the same parenting, and appear in relation to where the new parent is spawned. if you want the new children in different places you simply change their position to where you want after the instantiate.
Children always follow the parent. empty gameobjects still have transforms and follow all the parenting and positioning rules just like spawning any other model or rig.
if you need a code example for moving children let me know!
Hmm. So basically what your saying is they should be following my parent object anyway? I tested it with a cube that i added in to the scene and it worked fine, but it doesnt work with my group of objects. When I read over the api it said the children would take the position of the orginal objects that were cloned, but it did not specify if it was relative to the parent position or if it was hard cooridinates in world space. Based off what your saying it sounds like they should be relative. What could be a reason that it isnt working then? I just assumed it didnt appear to be working because the objects were just spawning at the exact position of the objects that were already in the scene. Whats a way to go about this? And a code sample on moving the children would be great so I can try that too lol.
Ok so I just looked again at what I was doing, and since I grouped all of these objects my vectors were off, it actually became
Instantiate(spawn, new Vector3(-205.4885f, -97.14504f, currentZ + 103), Quaternion.identity);
I completely overlooked that and just put zeros since my children were at zero, didn't even think of where my parent object actually was, and like you said, the children were automatically relative to it. Thanks a lot!
Your answer
Follow this Question
Related Questions
Why does my instatiate crashes editor? 1 Answer
How to make script affect only one this game object 2 Answers
Instantiated Bullets Not Picking Up Correct Position/Rotation 2 Answers
Instantiated clones using each other`s script 0 Answers
Making a shotgun shooting 45 degrees to the left of the mouse 1 Answer