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
3
Question by Burnzoire · Mar 31, 2011 at 11:32 PM · prefabnullchildrenpropertiesgetcomponentsinchildren

Access components in children of a prefab?

I have prefabs with a component say, "ClassA". These prefabs have multiple children with the component "ClassB". ClassB is used to store configuration properties that define the ClassA prefab. So the idea is that I can search through all of my Class A prefabs and check the properties on its children so I can determine which prefab I need. The reason I have used children to store these properties is because I wanted to use editor gizmos to make it easier to config each prefab (there are 27 children per prefab which would make it confusing to name & edit as standard public properties on the prefab).

The problem I'm having is that myPrefab.GetComponentsInChildren<ClassB>() is returning null, which really spoils my day. I don't want to have to instantiate all of my prefabs just to pick which one I need. Why is it that I can access properties on the prefab (ClassA) but not its children? Is there a way to achieve this without simply having all of my properties in ClassA?

Thanks in advance.

edit for clarity:

myPrefab - ClassA <-- I can access the component on the prefab just fine
+ child - ClassB <-- but I can't access the components in the prefab's children at all
+ child - ClassB
...
+ child - ClassB

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 3Eeng · Mar 05, 2012 at 12:12 AM 0
Share

Burnzoire, did you get an answer to this question? Your issue matches $$anonymous$$e very closely. Can you provide some insight to how you solved this? I am attempting to use an editor script to modify my prefabs and prefab children at edit time. but this does not seem possible without instantiating them, making the change, saving back the prefab, and finally deleting the instantiated version. even this is not working very well. Any help will make you a hero - in my $$anonymous$$d.

avatar image ne0r · Apr 27, 2012 at 01:02 AM 0
Share

Hey there,

i am having the exact problem. I need to access the hierachry of an non-instantiated prefab. Is there any chance to access the children?

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Jul 10, 2012 at 06:08 PM

GetComponentsInChildren has that already mentiond restriction to return only active gameobjects / components. Just iterate manually through them. Something like that:

 private static void ProcessChild<T>(Transform aObj, ref List<T> aList) where T : Component
 {
     T c = aObj.GetComponent<T>();
     if (c != null)
         aList.Add(c);
     foreach(Transform child in aObj)
         ProcessChild<T>(child,ref aList);
 }
 
 public static T[] GetAllChilds<T>(Transform aObj) where T : Component
 {
     List<T> result = new List<T>();
     ProcessChild<T>(aObj, ref result);
     return result.ToArray();
 }

You can of course implement it as extension methods to easily access them:

 public static T[] GetAllChilds<T>(this Transform aObj) where T : Component
 {
     List<T> result = new List<T>();
     ProcessChild<T>(aObj, ref result);
     return result.ToArray();
 }
 
 public static T[] GetAllChilds<T>(this GameObject aObj) where T : Component
 {
     List<T> result = new List<T>();
     ProcessChild<T>(aObj.transform, ref result);
     return result.ToArray();
 }
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 prak · Apr 24, 2013 at 04:16 PM 0
Share

I know this is an older post but, In my search for answer to a very similar problem as posted here I almost did not read this anwser after reading the first 2. But this is exactly what i was looking for. And hopefully others can find it too. thanks

avatar image
2

Answer by MasterLu · Aug 18, 2014 at 07:44 PM

I had the same Problem. Searching components from childObjects in a prefab via editorWindow returns null. My solution was using GetComponentsInChildren(true) to include nonActive childObjects.

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
1

Answer by DaveA · Mar 31, 2011 at 11:39 PM

PrefabA contains a script ClassA, and PrefabB contains a script ClassB, then you add PrefabB's to PrefabA (and make sure A stays a prefab), is that basically what you did? If so, the problem may be that you can't 'nest' prefabs and have them retain their lower-level individuality.

Other than that, maybe you can post some code and/or screenshot for us to review.

Comment
Add comment · Show 5 · 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 Burnzoire · Mar 31, 2011 at 11:53 PM 0
Share

No, ClassB is not a prefab. They are just components on the prefab's children.

To be more specific, each prefab is a building block that contains multiple children with Tag components. These tag components are used as meta data to deter$$anonymous$$e which blocks connect to each other.

avatar image Burnzoire · Apr 01, 2011 at 12:09 AM 0
Share

I'll post code when I'm at my computer later, but I was hoping the question was a bit simpler to explain! Simply put, 'myPrefab.getComponentsInChildren' doesn't appear tp work on an uninstantiated prefab. It seems that ONLY the components on the upper-most level of the prefab are accessible in an uninstantiated state?

avatar image DaveA · Apr 01, 2011 at 12:21 AM 0
Share

Ah ok. Being children, maybe they do need to be instantiated unless there's a way to 'dig into' the prefab itself, rather than an instantiation of it. You might instantiate one and make it inactive just to have around.

avatar image Burnzoire · Apr 01, 2011 at 02:48 AM 0
Share

Yeah it's for a real-time editor component of my game. So having every piece instantiated isn't ideal but I guess if they're inactive it might be acceptable.

avatar image Bezzy · Oct 14, 2014 at 02:01 PM 0
Share

It's actually fairly normal to have instantiated prefabs in your scene due to editor scripts (i.e. it's one way to do previews of 3D meshes via a Camera with a render texture target).

This is what gameObject.hideFlags is for. http://docs.unity3d.com/ScriptReference/HideFlags.html . Hope this gives you ideas! (It gave me ideas when I found out it was kosher!)

avatar image
0

Answer by Adam-Mechtley · Jul 10, 2012 at 05:25 PM

As far as I can tell, this is not possible.

The reason is because, as noted in the documentation, GetComponentsInChildren only returns components on active objects. Un-instantiated prefabs (i.e., assets in your asset database) are all deactivated (and must be—you get an error message saying that Unity will crash if you try to create an asset using an active GameObject). As such, it seems you must instantiate your prefab in a new scene to extract any needed data from children, or to make any necessary modifications to the source asset.

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 stevesan · Nov 25, 2013 at 05:14 PM

I'm having this issue too. My solution is to simply descend the hierarchy depth-first and call GetComponent on each child, looking for what I need. I guess GetComponent and GetComponentsInChildren behave differently unfortunately.

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

7 People are following this question.

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

Related Questions

Highlight complex gameobject 1 Answer

Master objects with preset connections to scene objects? 0 Answers

Best method for updating children of instantiated buttons? 0 Answers

Display one of prefab child 1 Answer

Game Object seems empty after instantiation 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