Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
8
Question by Kebabs · Dec 01, 2013 at 01:29 AM · gameobjectinstantiateprefabchildrenparenting

How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy

Hi I have a script which will Instantiate prefabs which works fine but I wanted them to be added to a gameobject that already exists and make the instantiated prefab a child of the already existing gameobject without making a new one. If it helps here is the script...

     if (randomizer == 1 && i <= 0)
     {
         Instantiate(A, new Vector3 (0,0,0), Quaternion.identity);
         System.Random random = new System.Random();
         randomizer = random.Next (1,5);
         i++;
     }

and an image of what gameobject I want the Instantiated objects to become children of. Thank you in advance and thank you for helping me with my studies. alt text

untitled-1.jpg (128.4 kB)
Comment
Add comment · Show 3
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 TrustTJM · Dec 01, 2013 at 02:32 AM 0
Share

$$anonymous$$ebabs is right! +1 It's a easy question. If you check API. You will get more

avatar image liam0neale · May 22, 2017 at 09:15 PM 0
Share

life saver u are

avatar image GDGames0302 · Feb 13, 2021 at 07:56 PM 0
Share

Hi. If you found the correct answer, please mark you question as Closed(from the edit button). Thank you.

3 Replies

· Add your reply
  • Sort: 
avatar image
16

Answer by robertbu · Dec 01, 2013 at 01:34 AM

If I understand your question correctly, you can replace line 3 with these two lines:

 GameObject go = Instantiate(A, new Vector3 (0,0,0), Quaternion.identity) as GameObject; 
 go.transform.parent = GameObject.Find("Stage Scroll").transform;
Comment
Add comment · Show 4 · 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 Kebabs · Dec 02, 2013 at 06:30 AM 0
Share

Thank you both for your help :)

avatar image JasonC001 · Oct 22, 2014 at 08:02 AM 0
Share

Thanks! Exactly what I was looking for.

avatar image Tunexxx · May 16, 2016 at 11:16 AM 0
Share

Can someone explain me, Instantiate function creates a "Object" variable Object class is parent to GameObject. So why are we able to cast Object as GameObject. I thought you can't cast children class with "as" it will return null if done so. So can somebody answer me why is it possible ? ;s

avatar image Bonfire-Boy Tunexxx · May 23, 2017 at 09:39 AM 0
Share

@Tunexxx The 'as' cast will only give you null if the thing you're casting cannot be cast to the class you're trying to cast it to. It can be used to check whether or not an object is of a particular (derived) type.

In most cases, when using Instantiate one knows what it is that one is instantiating, so this "null check" can safely be left out. But if you're not sure then yes, you should check for null immediately after casting, before trying to use the variable.

avatar image
0

Answer by d2clon · Feb 10, 2021 at 08:25 PM

Instantiate has several acceptations and a couple of them already accept the Transform element which will become the parent of the instantiate object, check the documentation:

  • https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

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 luthoff44 · Sep 04, 2021 at 08:03 PM

it has random position when instatianted lik -5.51 1.26 -35.37 how do i make it 0,0,0?

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

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

23 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 avatar image

Related Questions

How to instantiate within GameObject 1 Answer

Referencing gameObject from script after Instantiate 0 Answers

autolink to prefab? 0 Answers

Prefab instantiation: am I missing something? 1 Answer

How many time does a prefab takes to load?? 1 Answer


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