Swap layer, once again

Hi all,
I know that this has been discussed hundred times before but after a long search I didn’t find an answer to my problem: I have complex GameObjects made of children/granchildren/meshes/ etc…
I want to ignore raycast sometimes on these objects.
This code doesn’t work:

function maskObjectByName(name) {
 var o=GameObject.Find(name);
 var meshes = o.GetComponentsInChildren (Renderer);
 var children = o.GetComponentsInChildren (GameObject);
	
 for (var m : Renderer in meshes) { m.enabled = false; }
	
 o.layer = LayerMask.NameToLayer("Ignore Raycast");
 for (var child : GameObject in children) { child.layer = LayerMask.NameToLayer("Ignore Raycast"); }
}

Only the top GO is moved to layer 2, not his children.
Why ?

GameObject isn’t a component. A GameObject can have components, but it’s not one itself. You can do:

var meshes = o.GetComponentsInChildren (Renderer); 
for (var m : Renderer in meshes) {
   m.enabled = false;
   m.gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");
}

You don’t need to set the layer for the parent separately, because GetComponentsInChildren(Renderer) includes the parent. (Unless the parent doesn’t have a renderer, in which case it won’t be included.)

–Eric

Hi,
Thanks a million, it works like a charm!