- Home /
Instantiate as a Child of the Parent
This is probably an easy question but I have tried all permutations I can think of and its not working.
I want to instantiate 'poisonSmoke' as a child of the game object being poisoned so the particle effect will follow the game object. But no matter what I try it wont parent it.
Any help would be appreciated.
Thanks
Instantiate (poisonSmoke, Vector3(transform.position.x,transform.position.y, transform.position.z) , Quaternion.identity);
poisonSmoke.transform.parent = gameObject.transform;
And that script is attached to the thing that has been poisoned?
Answer by whydoidoit · May 30, 2012 at 11:12 PM
You need to get the thing that is created by Instantiate and set that one's parent, at the moment you are setting the prefab's parent!
var myNewSmoke = Instantiate (poisonSmoke, Vector3(transform.position.x,transform.position.y, transform.position.z) , Quaternion.identity);
myNewSmoke.transform.parent = gameObject.transform;
Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
isn't this a better solution now?
This might have been the only way back when this question was asked. Nowadays Instantiate() takes a parent object as a parameter. Do NOT use the above approach in new code as the reparenting costs performance and buys you nothing.
If you would have given an example of how t do it properly then you would increase your answers value a lot , as "newbies" like me dont get what you say at all :D So feel free to post one ^^ As I ran into that problem right now and dont know how to do it yet :)
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
Remember that it has to be new Vector3 since you dont have a vector yet
Answer by Denaton · Feb 03, 2017 at 02:46 PM
You can add it at the same time you Instantiate it by doing this:
Instantiate (poisonSmoke, transform);
I find this the cleanest way.
Perfect. I like this because it doesn't mess with the scale of the prefab it the parent scale is different.
Answer by d2clon · Aug 23, 2021 at 12:51 PM
The up to date solution is hidden in the comments so I post it here as an answer:
Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent)
Answer by reptilebeats · May 30, 2012 at 10:39 PM
have a look at this on how to access children http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html
Yeah that is not possible as I have over 200+ objects all called the same name with the same tag.
It's Extremely crude, but this may work (It did got me)
this.name = "Tree" + Random.Range(-80000000.0f, 800000000.0f).ToString();
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Instantiated GameObject collision without script repetition? 1 Answer
How to change position of child component without affecting the position of the parent 1 Answer
Instantiate a game object in array then change parent 1 Answer
Prefab, procedural attachment of child, childCount return 0 1 Answer