Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by dhawalbanker · Aug 13, 2013 at 07:24 AM · prefabloadnestedresource

How to load nested prefab?

I have a Prefab. ParentPrefab, which contains child1, child2 etc.

I want to access child1 from my C# scripts.

I am able load ParentPrefab using Resource.Load("ParentPrefab"). But I dont know how to access ParentPrefab.child1 in some GameObject variable in C# script.

Comment
Add comment · Show 1
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 Joyrider · Aug 13, 2013 at 07:27 AM 0
Share

So you have nested prefabs, but only want to load the childPrefab?

Have you tried loading the childPrefab directly Resource.Load("childPrefab") ? (I guess you already tried that)

$$anonymous$$y guess is you'll have to load the parent prefab and then access the child with loadedParentPrefab.GetChild(#)

1 Reply

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

Answer by dorpeleg · Aug 13, 2013 at 07:28 AM

You need to do something like this:

 GameObject parentObject = Resources.Load("ParentPrefab");

Then, you can use one of the following ways to get the child:

Transform.Find

 GameObject childObject = parentObject.transform.Find("child name");

Transform.GetChild

 GameObject childObject = parentObject.transform.GetChild(child index);


I don't think it's possible to load only the child from the resources.

Comment
Add comment · Show 10 · 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 dhawalbanker · Aug 13, 2013 at 10:24 AM 0
Share

Hey, for below syntax

GameObject childObject = parentObject.transform.Find("child name");

I see following error:

error CS0030: Cannot convert type UnityEngine.Transform' to UnityEngine.GameObject'

avatar image dhawalbanker · Aug 13, 2013 at 10:25 AM 0
Share

GameObject childObject = parentObject.transform.Find("child name");

error CS0030: Cannot convert type UnityEngine.Transform' to UnityEngine.GameObject'
avatar image Joyrider · Aug 13, 2013 at 10:30 AM 0
Share

the result of a Find is a Transform, not a GameObject, so you should either get the gameObject of the result of your find

 GameObject childObject = parentObject.transform.Find("child name").gameObject;

or change the variable to a Transform class variable

 Transform childTransform = parentObject.transform.Find("child name");
avatar image dhawalbanker · Aug 13, 2013 at 10:39 AM 0
Share

Thanks. That worked.

Can you also point me to nice coding tutorial for Unity?

avatar image Joyrider · Aug 13, 2013 at 10:46 AM 0
Share

Okay, great, accept dhawalbanker's answer then ;) So this question can be closed and added to the knowledge base.

And there are many tutorials out there, it all depends what you want to do. Look around on answers and on the forum, do a google search ^^

You'll find some Unity(not necessarily code-oriented) tutorials here

Show more comments

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

17 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

Related Questions

Import a prefab from file at runtime? 1 Answer

How to save a prefab when a button is pressed 1 Answer

load objects just if they in range of camera 0 Answers

How do I find a Prefab after I Replaced said Prefab? 1 Answer

Visualization of saved games inside a Load Menu - Problem with instantiated prefabs 0 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