- Home /
Disabling all renderers in Hierarchy
var renders : Renderer[] = gameObject.GetComponentsInChildren(Renderer);
Debug.Log("Num renderers: " + renders.length);
for (var rend : Renderer in renders) {
rend.enabled = false;
}
This throws an error:
InvalidCastException: Cannot cast from source type to destination type.
ChangeSize.Start () (at Assets/Scripts/AboveCode.js:1)
Excuse me, but Renderer is of type Component, yes you CAN make that cast. I mean, hell, that code is copied from the documentation's example on using GetComponentsInChildren, only using the Renderer instead of the HingeJoint (which also inherits from type Component).
Answer by AlucardJay · Apr 01, 2013 at 08:11 PM
I had the same problem, but found this (check the last post) : http://forum.unity3d.com/threads/143049-Need-to-disable-all-Renders-in-Children-Objects
gameObject.GetComponentsInChildren.< Renderer >();
Try using this syntax :
function DisableAllRenderers()
{
var allRenderers : Renderer[];
allRenderers = gameObject.GetComponentsInChildren.< Renderer >();
for ( var childRenderer : Renderer in allRenderers )
{
childRenderer.enabled = false;
}
}
Trying that now. And... No compile errors, but it's not disabling them on start. The same function call later in the program (that hides the model after it's "closed") does function correctly.
Try function Awake() ins$$anonymous$$d of Start, maybe the object has not yet initialized (just a guess).
Or try Invoke( "DisableAllRenderers", 0.02 );
Nope. Didn't work there either. I can see how many renderers there are (154--I need to get my modeler to reduce that number!), so it is running, it's just not disabling them.
Edit:
Ok this is weird. I manually disabled them all so that they were disabled by default.
Something turned them on. o..O
Redit: Oh, I know what's doing it. $$anonymous$$ram AR plugin. >..<
That is good you found the problem and what was causing it. I was a little worried that you had this not disabling on start problem, when you could disable once everything was running. Definitely if another script is enabling them, there's the problem. So my answer with the correct syntax for finding all children renderers is proven working now I hope.
Yeah, I forgot that the AR plugin turns off all the renderers at start (but oddly, when it loses its tracker, it doesn't disable them again).
Your answer
Follow this Question
Related Questions
About "SplineController" Javascript 1 Answer
What is wrong with this script? 0 Answers
If statement only work once? 1 Answer
how to use matrice with unity 3D 1 Answer
[Solved] Generating Particles with Attack Animation 1 Answer