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
1
Question by Fabkins · Nov 06, 2011 at 12:52 PM · meshfiltergetcomponentinchildrengetcomponentsinchildren

Problems with finding all children that have meshes

I am looking for all objects in a hierarchy that have meshes. This includes the object itself and any of its children. The purpose is to attach a mesh effect.

If I use:

 var mesh : MeshFilter = gameObject.GetComponentInChildren(MeshFilter);
 mesh.gameObject.AddComponent ("MeshExplode");

It finds the first mesh and works fine. But as I want to have all meshes I am trying the following:

 var meshes : MeshFilter[] = gameObject.GetComponentsInChildren(MeshFilter) as MeshFilter[];
             
 if(meshes)
 {
   for (var mesh : MeshFilter in meshes) 
   {
      Debug.Log("Found mesh =======================================");
      mesh.gameObject.AddComponent ("MeshExplode");
   }
 }
 else
 {
   Debug.Log("No meshes");
 }

This doesnt find any meshes and I am completely confused to why.

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

2 Replies

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

Answer by Fabkins · Nov 06, 2011 at 02:06 PM

Ok, I've found my own answer. I put the result of the find straight into the for loop. I have no idea why it didnt work when the result in an array but the following works a treat:

 for (var mesh : MeshFilter in gameObject.GetComponentsInChildren(MeshFilter) )
 {
 Debug.Log("Found mesh =======================================");
 mesh.gameObject.AddComponent ("MeshExplode");
 }
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 syclamoth · Nov 06, 2011 at 01:20 PM

Is it even getting to the for loop? It's possible that you can't cast a MeshFilter[] to a boolean, and so

 if(meshes)

always returns false! Try

 if(meshes.Length > 0)

instead, see if that works any better.

Comment
Add comment · Show 3 · 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 Fabkins · Nov 06, 2011 at 01:26 PM 0
Share

That gets me:

NullReferenceException: Object reference not set to an instance of an object

Its driving me buggy.....

In theory I shouldnt even need the if condition statement, I ought to be able to drop straight into the loop but produces the same as the above error....

(worth a try though)

avatar image syclamoth · Nov 06, 2011 at 01:33 PM 0
Share

Well, that means that meshes isn't getting assigned properly! Is all of this happening inside a function? I'm afraid I can't really help you much more here, I'm not that good at JavaScript (I could help you if it were C# though)

avatar image Fabkins · Nov 06, 2011 at 01:40 PM 0
Share

Hi syclamoth, yes its happening in a function. The bit I just dont understand is why getting the first instance works but getting an array of all of them doesnt.

Thanks for your suggestions anyhow.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I retrieve deep levels of nested child transforms with a generic snippet of code 2 Answers

How do I access Mesh/ Meshfilter in Editor code? 3 Answers

GetComponentInChildren is getting Sibling s Child 0 Answers

WHy cant I create the GetComponentsInChildren(Renderer) array? 2 Answers

How to get script from Object with (Clone)'s script? 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