- Home /
Parenting Instantiated Objects
Pretty simple question.
Is there a way to instantiate an object and attach it to a Player Object? We are doing a RPG and I have a plane with texture for magic casting. Now it spawns and does everything correct, however if the player moves while doing it, it doesn't move like I'd like even with the code I have. So my question is, can I instantiate an object and attach it to the Player Object? This would fix the object as it would move according to the Player setup.
Thanks
Answer by Cyclops · Mar 14, 2010 at 03:36 PM
By object, I presume you mean GameObject, and not Component (which would require AddComponent()). If you're instantiating a new GameObject, and want to set its parent to another GameObject, what you would do is just set the parent property to the owner object's transform:
public GameObject prefabThing; // drag/drop a reference to the object.
GameObject newThing = (GameObject) GameObject.Instantiate(prefabThing);
newThing.transform.parent = PlayerGO.transform;
Thank you! For some reason I missed the transform.parent thing. LOL I feel like a fool :) Thanks again.
It's easy to miss. It took me some time to get used to the hierarchy of GameObject->transform, where the GameObject really doesn't do much of anything... :)