• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
25
Question by mabit · May 30, 2012 at 10:33 PM · instantiateparentchild

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;
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 30, 2012 at 11:10 PM 0
Share

And that script is attached to the thing that has been poisoned?

avatar image whydoidoit · May 30, 2012 at 11:11 PM 0
Share

Oh sorry forget that I see your problem :)

4 Replies

· Add your reply
  • Sort: 
avatar image
78
Best Answer

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;
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mabit · May 31, 2012 at 09:11 PM 1
Share

Thanks a lot for the easy answer

avatar image Suhrahj · Dec 08, 2016 at 02:28 PM 12
Share

Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);

isn't this a better solution now?

avatar image Cross22 · May 19, 2017 at 04:10 AM 5
Share

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.

avatar image HydroxTV Cross22 · May 23, 2017 at 10:06 AM 1
Share

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 :)

avatar image BadSeedProductions HydroxTV · Aug 23, 2017 at 06:20 PM 5
Share

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);

avatar image simon_hen_1 · Jun 10, 2021 at 10:30 AM 0
Share

Remember that it has to be new Vector3 since you dont have a vector yet

avatar image
50

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image g-hoot · Feb 09, 2018 at 09:11 PM 0
Share

Perfect. I like this because it doesn't mess with the scale of the prefab it the parent scale is different.

avatar image
12

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)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mabit · May 30, 2012 at 11:07 PM 0
Share

Yeah that is not possible as I have over 200+ objects all called the same name with the same tag.

avatar image ConsumerDev mabit · Aug 03, 2016 at 04:17 PM 0
Share

It's Extremely crude, but this may work (It did got me)

 this.name = "Tree" + Random.Range(-80000000.0f, 800000000.0f).ToString();

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Getting instance of an sub object rather than the original's subobject 0 Answers

Instantiate GameObject Parent targetting issue. 0 Answers

[SOLVED] How to make to clone an object and make it parent of the transform? 1 Answer

Give prefab a parent 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges