x


WHy cant I create the GetComponentsInChildren(Renderer) array?

I get the following error at this line: InvalidCastException: Cannot cast from source type to destination type. I'm really not sure why, as the code emulates the documentation perfectly. Is theresomething unique about renderer types?

var objectRenderers : Renderer[];
objectRenderers=downloadedAssetBundle.GetComponentsInChildren(Renderer);

Note: downloadedAssetBundle is an instantiated mainAsset (GameObject) from an asset bundle.

more ▼

asked Feb 09 '12 at 08:14 AM

grimmy gravatar image

grimmy
302 22 34 43

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

This is why I hate UnityScript. GetComponentsInChildren return type is Component[], and objectRenderers is Renderer[]. The compiler should be smart and try to do a cast automatically or at least show a compile-time error stating that the types don't match. But no, it accepts your code and crash at runtime... < / rant>

A manual cast to Renderer[] should work:

var objectRenderers : Renderer[];
objectRenderers=downloadedAssetBundle.GetComponentsInChildren(Renderer) as Renderer[];
more ▼

answered Feb 09 '12 at 06:59 PM

luizgpa gravatar image

luizgpa
851 4 9

Well this manages to get past the initial error but then when I get to the next line which reads for (var eachPart : Renderer in objectRenderers) --(exactly the same as documentation) it crashes because it looks like the objectRenderers array doesn't actually exist. Any idea? Thanks!

Feb 10 '12 at 03:45 PM grimmy

My bad. The code I posted actually doesn't work. Apparently its not possible to cast the entire array, but you can cast individual element of it in the "foreach" loop.

//objectRenderers will be a Component[]
var objectRenderers = downloadedAssetBundle.GetComponentsInChildren(Renderer);

for (var eachPart : Renderer in objectRenderers) {
...
Feb 10 '12 at 04:30 PM luizgpa

Huh? That's the code I had originally which didn't work..Did you miss something? I tried doing [code] var objectRenderers = downloadedAssetBundle.GetComponentsInChildren(Renderer); for (var eachPart : Renderer in objectRenderers as Renderer)
{..[/code] but that didnt work either :(

Feb 10 '12 at 05:06 PM grimmy

Not really. This way I'm not declaring the type of objectRenderers and letting the compiler choose one. In this case it will be Component[].

Sometimes UnityScript behaves differently between versions. What version of Unity are you using?

Feb 10 '12 at 05:31 PM luizgpa

Ahha. I removed the declaration and it worked!! Many thanks!!

Feb 10 '12 at 05:48 PM grimmy
(comments are locked)
10|3000 characters needed characters left

Here's the final piece of working code. Note: objectRenderers is never declared. If you do declare it , it will crash.

objectRenderers=downloadedAssetBundle.GetComponentsInChildren( Renderer );

for (var eachPart : Renderer  in objectRenderers ) //as Renderer??
    {
       //set a transparent shader
       eachPart.sharedMaterial.shader = Shader.Find ("Transparent/Diffuse");
       eachPart.sharedMaterial.color = Color.blue;
       eachPart.sharedMaterial.color.a=0.3;
    }
more ▼

answered Feb 10 '12 at 05:50 PM

grimmy gravatar image

grimmy
302 22 34 43

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x382
x275
x21

asked: Feb 09 '12 at 08:14 AM

Seen: 930 times

Last Updated: Feb 10 '12 at 05:50 PM