Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by grimmy · Feb 09, 2012 at 08:14 AM · assetbundlerenderergetcomponentinchildren

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by luizgpa · Feb 09, 2012 at 06:59 PM

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...

A manual cast to Renderer[] should work:

 var objectRenderers : Renderer[];
 objectRenderers=downloadedAssetBundle.GetComponentsInChildren(Renderer) as Renderer[];
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image grimmy · Feb 10, 2012 at 03:45 PM 0
Share

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!

avatar image luizgpa · Feb 10, 2012 at 04:30 PM 0
Share

$$anonymous$$y 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) {
 ...
avatar image grimmy · Feb 10, 2012 at 05:06 PM 0
Share

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 :(

avatar image luizgpa · Feb 10, 2012 at 05:31 PM 0
Share

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?

avatar image grimmy · Feb 10, 2012 at 05:48 PM 0
Share

Ahha. I removed the declaration and it worked!! $$anonymous$$any thanks!!

avatar image
0

Answer by grimmy · Feb 10, 2012 at 05:50 PM

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;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I access the renderer from a child? 1 Answer

Do something for each material in each renderer in children... 2 Answers

GetComponentInChildren< Renderer > failing to find the renderer 1 Answer

how do I change the properties of a material that are on the children of an object on script? 1 Answer

Disable multiple child components 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges