- Home /
Get SkinnedMeshRenderer as Renderer?
I'm trying to access all the renderers of my gameObject like this:
var renderers : Renderer[] = GetComponentsInChildren(Renderer) as Renderer[];
But I get null.
As the manual states, SkinnedMeshRenderer inherits from Renderer, so this should be possible, isn't it?
Answer by skovacs1 · Sep 29, 2010 at 07:08 PM
GetComponentsInChildren(Renderer) is returning you an array of all Renderer components, including the derived types.
The problem is your attempt to convert the array of Components to an array of Renderers. Not Sure why this isn't behaving, but it may have something to do with a lack of such a conversion.
Your code will work if it looks more like:
var renderers : Component[] = GetComponentsInChildren(Renderer);
for(var currentRenderer : Renderer in renderers) {
//do something with currentRenderer.
}
Thank you, it works. Although it should be possible to cast the array, in my opinion.
Believe me when I say that I know how you feel, but it seems like for whatever reason no such conversion exists. I would have thought it would have worked too, but I'm sure there's a reason that it doesn't. $$anonymous$$aybe it has to do with some $$anonymous$$utia of the languages in use or maybe it's something that the Unity SEs didn't implement, be it intentionally or not.
It certainly is weird given as the singular version's return type can be cast.
Your answer
Follow this Question
Related Questions
Swapping out a single material on a SkinnedMeshRenderer at runtime 0 Answers
SkinnedMeshRenderer and android problem 1 Answer
Help! Skinned mesh render not visible!! 3 Answers
Need to set updateWhenOffscreen, but no SkinnedMeshRenderer is present 1 Answer
SkinnedMeshRenderer not being affected by lights with OpenGL ES 1.x 0 Answers