Thats because safecasting(‘as’) doesnt handle arrays to well in my experience. GameObject.GetComponents() returns a Components[ ]. Just rewrite your this line Renderer[] ChildrenRenderer = ObjParent.GetComponentsInChildren(typeof(Renderer)) as Renderer[];
Assets/Script/main.cs(66,24): error CS0266: Cannot implicitly convert type UnityEngine.Component[ ]' to UnityEngine.Renderer[ ]'. An explicit conversion exists (are you missing a cast?)
and when i add “as Renderer[ ]”,it says:
NullReferenceException: Object reference not set to an instance of an object
I think Slem made a typo. Because GetComponents() returns Component[ ], ChildrenRenderer has to be of type Component[ ]. The foreach loop will still work as written, because it will cast each Renderer properly as it iterates.
How can one do this in the editor scripting side, where there is no transform property? I have an fbx model that I want to do something to all of its children, but there are too many to do it all manually.