Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 almo · Jun 01, 2011 at 08:49 PM · javascriptinvalidcastexceptiongetcomponentsinchildren

InvalidCastException in Javascript

The error I get is this:

InvalidCastException: Cannot cast from source type to destination type. TestOffMeshLinkActivation.Start () (at Assets/Scripts/TestOffMeshLinkActivation.js:13)

The code:

 private var m_OML1 : OffMeshLink;
 private var m_OML1Renderers : Renderer[];
 
 function Start()
 {
     var list : GameObject[];
     list = GameObject.FindGameObjectsWithTag("OML1");
     m_OML1 = list[0].GetComponent(OffMeshLink);
     m_OML1Renderers = list[0].GetComponentsInChildren(Renderer);
 }

The error happens on the line where it does GetComponentsInChildren. As far as I can tell, my code is exactly like the example here: http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentsInChildren.html

I must be missing something silly, or I don't really understand how GetComponentsInChildren is supposed to work.

The FindGameObjectsWithTag is functioning, because I'm able to use the m_OML1 object with no trouble.

Comment
Add comment · Show 1
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 almo · Jun 01, 2011 at 09:12 PM 0
Share

Update: GetComponentsInChildren(Renderer) is returning something of type Component[]. Why is that? Shouldn't it be of type Renderer[]?

2 Replies

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

Answer by iggy · Jun 01, 2011 at 09:34 PM

**GetComponentsInChildren(Renderer)**
returns Component[]


**GetComponentsInChildren. ()** //without spaces, i had to enter spaces because of this HTML text decoder
returns Renderer[]
Comment
Add comment · Show 6 · 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 almo · Jun 02, 2011 at 01:14 PM 0
Share

Are you saying the example at the following URL is incorrect?

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentsInChildren.html

I'm in Javascript, so I can't use the version with greater-than and less-than signs.

avatar image almo · Jun 02, 2011 at 01:21 PM 0
Share

This answer

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

Says this:

// In Javascript, the "GetComponent" function returns an object, which is automatically cast by Javascript to the correct type

var someScript : ExampleScript = GetComponent(ExampleScript);

avatar image Eric5h5 · Jun 02, 2011 at 01:58 PM 1
Share

@almo: the example in that URL is correct. As iggy said, it returns Component[]. And the code iggy wrote--`GetComponentsInChildren.()`--is Javascript, and wouldn't compile in C# (which would be GetComponentsInChildren<Renderer>()). Your code isn't like the example; you're trying to cast Component[] to Renderer[].

avatar image almo · Jun 02, 2011 at 02:40 PM 0
Share

Ok, I get the GetComponentsInChildren.<Renderer>() syntax now. Thanks! But I still don't see the difference between the code I posted and the example at the URL. I have private var m_O$$anonymous$$L1Renderers : Renderer[]; and m_O$$anonymous$$L1Renderers = list[0].GetComponentsInChildren(Renderer); The example has var hingeJoints : HingeJoint[]; and hingeJoints = gameObject.GetComponentsInChildren(HingeJoint);

avatar image Eric5h5 · Jun 02, 2011 at 02:57 PM 1
Share

@almo: oops, yeah, I missed that part. In fact, that example in the docs is wrong and won't work. It should use hingeJoints = gameObject.GetComponentsInChildren.<HingeJoint>(); ins$$anonymous$$d.

Show more comments
avatar image
-1
Wiki

Answer by perchik · Jul 31, 2013 at 04:52 PM

Actually the docs a̶r̶e̶n̶'̶t are wrong. For Javascript you don't have to use the angle brackets and pass in a type. The reason the documentation version is almost right is the following line :

 for (var joint : HingeJoint in hingeJoints)

That line takes each Component item from hingeJoints and implicitly casts them into the HingeJoint type.

The documentation clearly says

GetComponentsInChildren(type: Type): Component[];

which means the function returns an array of Components. Javascript allows you to cast on the fly, which makes it very sloppy and prone to errors, but the documentation is correct.

Revision


Revised after all the comments on my answer.

The documentation is wrong, but not for the reasons argued. The documentation has the line var hingeJoints : HingeJoint[]; which breaks, because it tries to implicitly convert an array of components to HingeJoint[]. If you changed it to var hingeJoints; it would work exactly right.

I made a dummy scene to illustrate why the rest of it works. TestObject has "test.js" on it, and three children, a,b,c, with Mesh Renderers on them.

alt text

test.js:

 #pragma strict
 
 function Start () { 
     var renders = gameObject.GetComponentsInChildren(MeshRenderer); //Component[]
     for(var renderer:MeshRenderer in renders){ //implict cast on this line
         Debug.Log(renderer.name);
     }
 }

When I click play, it prints out a, b, c to the log (so it is finding each of the renderers correctly).

The reason this works is implicit casting. If I were to explicitly cast everything, it would look like this:

 #pragma strict
 
 function Start () { 
     var renders:Component[];
     renders = gameObject.GetComponentsInChildren(MeshRenderer);
     for(var comp:Component in renders){ //get each object as a component
         var renderer : MeshRenderer;  
         renderer= comp as MeshRenderer; //cast the component to a MeshRenderer
         Debug.Log(renderer.name);
     }
 }


I'm not saying this is the right way. I'm definitely not advocating for this because it's incredibly sloppy and implicit type casting causes lots of problems (as noted). All I am saying is that the syntax of passing a type through parentheses works and you don't have to use the generic version.


testsetup.png (13.4 kB)
Comment
Add comment · Show 12 · 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 Cascho01 · Jul 31, 2013 at 05:00 PM 0
Share

Impossible.

$$anonymous$$y script was causing an error when I used

(...)

ins$$anonymous$$d of

<...>()

avatar image perchik · Jul 31, 2013 at 05:04 PM 0
Share

In javascript?

avatar image Eric5h5 · Jul 31, 2013 at 05:09 PM 0
Share

No, the docs are really not correct. Did you actually try it? It will fail, even without #pragma strict, which disallows casting on the fly. (And all example code needs to be compliant with #pragma strict, since it's forced in many cases—iOS etc.—and can't be considered optional. Not to mention the standard script template for JS includes #pragma strict by default. Which is irrelevant since the example code is just plain wrong regardless. ;) )

avatar image Cascho01 · Jul 31, 2013 at 05:10 PM 0
Share

using javascript on ios with pragma strict

I stumbled often on this problem

everything works fine with <..>() and does not work with (...)

avatar image perchik · Jul 31, 2013 at 05:19 PM 1
Share

I store the renders as a var. If you wanted to be explicit, you could do this:

     var renders:Component[];
     renders = gameObject.GetComponentsInChildren($$anonymous$$eshRenderer);
     for(var renderer:$$anonymous$$eshRenderer in renders){
         Debug.Log(renderer.name);
     }

Line 1 creates renders as an array of Components

Line 2 Get all of the $$anonymous$$eshRenderer components and stores it as a component array

Line 3 says for each $$anonymous$$eshRenderer in the renders array do something. This is where the implicit conversion is, because each item in renders is a Component, but I implicitly cast it to a $$anonymous$$eshRenderer, which is exactly how the Documentation page does it

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

InvalidCastException error and logic error 1 Answer

InvalidCastException when trying to access child components 2 Answers

short script error mesige 1 Answer

InvalidCastException: Cannot cast from source type to destination type. ??? 2 Answers

Sytem.InvalidCastException on Windows Phone 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