x


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.

more ▼

asked Nov 06 '11 at 12:52 PM

Fabkins gravatar image

Fabkins
675 15 20 26

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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");
}
more ▼

answered Nov 06 '11 at 02:06 PM

Fabkins gravatar image

Fabkins
675 15 20 26

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 06 '11 at 01:20 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

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)

Nov 06 '11 at 01:26 PM Fabkins

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)

Nov 06 '11 at 01:33 PM syclamoth

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.

Nov 06 '11 at 01:40 PM Fabkins
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x64
x24
x21

asked: Nov 06 '11 at 12:52 PM

Seen: 942 times

Last Updated: Nov 06 '11 at 02:06 PM