Mesh renderer disabled for all children.

So I can’t figure out how to do such thing. I have a weapon with about 12 children inside it and I want to set each child’s mesh renderer to false, how do I do this with a script. I can do it one by one for each child in the code, but how would I do it for all the children ?

Would the GameObject.SetActiveRecursively command suffice for your purposes? It will disable an object and all of its children.

Well not exactly I want the weapon to work, just not to show. I’m doing a sniper zoom and just need to hide the sniper while zooming.

something like this ?

Renderer[] rs = GetComponentsInChildren<Renderer>()
foreach(Renderer r in rs)
     r.enabled = false;
3 Likes

Yeah, but I didn’t get what Renderer[ ] and where “r” comes from.

Renderer[ ] rs - rs is an array of Renderer references.
Renderer r - r is one Renderer.

So I’ll have to predefine all renderers in variables before I do that, right ? That wouldn’t be really nice, cuz the gun has like a hundred children.

No, GetComponentsInChildren is a built in method that will retrieve for you all the child renderers without you having predefined them (they do have to be assigned as children though at the time). All you should need in your code is what appels put; those variables are declared locally/temporarily:

Renderer[] rs = GetComponentsInChildren<Renderer>()
foreach(Renderer r in rs)
     r.enabled = false;

I’ve never had to do this before, but from what I’ve done with renderers, I believe this should work fine. Do you have nested children? That is, children of children of children and so on within your gun?

GetComponentsInChildren documentation: Unity - Scripting API: Component.GetComponentsInChildren

1 Like

Yes, I do. Take a look at the picture. I couldn’t get it to work that way :frowning:

Then write the method recursively.

EDIT: or maybe something like this (written in notepad, untested, etc.):

Stack<Transform> children = new Stack<Transform>();
children.Push(this.transform);
while(children.Count > 0)
{
	Transform current = children.Pop();
	Renderer renderer = current.GetComponent<Renderer>();
	if (renderer != null)
	{
		renderer.enabled = false;
	}
	foreach(Transform child in current.transform)
	{
		children.Push(child);
	}
}

GetComponentsInChildren() or (X) returns all X objects in the current gameObject and all of its children.
GetComponentInChildren (note the missing s) only returns the first one.

What problem did you have with GetComponents? I have that exact code snippet working on my characters to pick only certain meshes to display.

Well, I don’t know why, but it expects three semicolons on only one row and expects brackets on strange places. I just copied the code and declared the “r” variable as a Renderer variable.

Then you must have a typo in your syntax. 3 semi-colons on a row?

Are you using JavaScript/UnityScript or C#?

I’m using Javascript and there’s not a possible semicolon missed, there’s only two - one after Renderer[ ] rs = GetComponentsInChildren() and another after r.enabled = false.

Well, we’re posting C# code so you have to convert it to a UnityScript equivalent.

EDIT: 'cause I know where this is going to lead…

for(var r : Renderer in GetComponentsInChildren(Renderer))
{
     r.enabled = false;
}

I think this will work, but I’m not sure how the new type-safety rules in UnityScript work.

If Unityscript even supports generics, it’s not in that format. is C#-only syntax. You need to use
GetComponentsInChildren(Renderer).

Ninja’d.

I’ve heard UScript supports generics; if anyone knows how, I’d love to know. :x

From what I’ve gathered from tidbits of forum posts, it seems like some of the API has been converted to support generics, but not all.

embarrassing… I really felt, that there’s something wrong with the code you gave me, but than I though that it’s some wicked syntax, that I haven’t heard of :smile:

Now the code makes sense. Thanks!

It is. It is wicked syntax.

1 Like

this is perfect - this code still working in unity 4.5
all it was missing was a semi-colon added at the end of the first line.

Renderer[] rs = GetComponentsInChildren<Renderer>();
foreach(Renderer r in rs)
     r.enabled = false;

I added this to the start function of a script attached to the head object of the player prefab to disable a local players head, eyes, nose and mouth and hair mesh renderer, left all other components intact, much better method than just disabling the children