• 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
0
Question by avnerpower · Jan 20, 2016 at 06:35 PM · prefabprefabschild

How do I access a prefab inside a prefab inside a prefab?

As you can see in these screenshots: https://gyazo.com/abd4a852e2df8943dfd74210c7b8fb7e https://gyazo.com/37bfbd1a9d1f8769a4c1a0aa3bf356bb I want to access the children of my prefab, that are also prefabs, but I can't. How do I do that?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JoshuaMcKenzie · Jan 20, 2016 at 07:27 PM

hate to break it to you, but you can't save nested prefabs, once you do all the nested prefabs instances break connections to their prefabs and just become a part of the parent prefab.

what you can do instead is just have the Parent prefab with a script. This script will have a public reference to the child prefab so you can drag the childprefab into the field in the inspector. so when Awake is called you can just instantiate the child prefab and save the result to a gameobject reference in the same script.

 public class ParentScript : Monobehavior
 {
     public GameObject childPrefab;
     public GameObject childInstance;
 
     void Awake()
     {
         childInstance = Instantiate<GameObject> (childPrefab);
         childInstance.transform.parent = transform;
         childInstance.transform.localPosition = Vector3.zero;
     }
 }

you can repeat this ad infinitum for each prefab. Though its prolly best not to overdo it, the parent's Awake won't finish until all the child ones do. and theres just so much you can instantiate in a single frame, especially if your developing for mobile (In which case you should use Object pooling)

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 mrEagle · Aug 15, 2016 at 10:48 PM 0
Share

Thanks, this really helped me! It is a simple solution compared to integrating some third party asset. It keeps the folders clean and can be rolled out by simply adhering to the pattern for new assets.

I tend to use [ExecuteInEdit$$anonymous$$ode] and destroy the ParentScript component on Awake after spawning the children to make it look more like dragging in an actual prefab rather than only the top parent.

Bonus points for solving questions like circular references :-)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Separate different kind of prefab/enemy ? 0 Answers

Script is reading from prefab instead of instance 1 Answer

Prefab mode doesn't show prefab, still shows old scene. 7 Answers

Putting a variable of a child in the inspector for the parent 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