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 entity476 · Jun 09, 2013 at 03:23 PM · arraymeshfilterinvalidcastexceptionbuiltin array

"InvalidCastException" with MeshFilter from JS Array to builtin array.

I apologise, if my problem will not turn to be of common interest, but I try to repeat what is suggested by a piece of code in Unity Scr. Ref. Moreover, I understand that I am limited by significant lack of knowledge in coding and I'm not sure if the data I am providing are enough to locate the problem. I searched for a while in similar error cases, but I don't think I can figure out what's going on with mine.

Well, similarly to this Unity example:

 var array = new Array (Vector3(0, 0, 0), Vector3(0, 0, 1));
 array.Push(Vector3(0, 0, 2));
 array.Push(Vector3(0, 0, 3));
  // Copy the js array into a builtin array
 var builtinArray : Vector3[] = array.ToBuiltin(Vector3) as Vector3[];

I do the following:

 var unappliedMeshes = new Array(MeshFilter);
 unappliedMeshes.Push(unappliedMesh); //unappliedMesh is a MeshFilter
 var builtinUnappliedMeshes : MeshFilter[] = unappliedMeshes.ToBuiltin(MeshFilter) as MeshFilter[];

But I get the following error message:

InvalidCastException: Cannot cast from source type to destination type. System.Array.Copy (System.Array sourceArray, Int32 sourceIndex, System.Array destinationArray, Int32 destinationIndex, Int32 length) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System/Array.cs:997) System.Array.Copy (System.Array sourceArray, System.Array destinationArray, Int32 length) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System/Array.cs:936) System.Collections.ArrayList.CopyTo (System.Array array) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Collections/ArrayList.cs:3053) System.Collections.ArrayList.ToArray (System.Type type) (at /Applications/buildAgent/work/b59ae78cff80e584/mcs/class/corlib/System.Collections/ArrayList.cs:3219) UnityScript.Lang.Array.ToBuiltin (System.Type type) deformation.FixedUpdate () (at Assets/Scripts Javascript/deformation.js:92)

I have not, of course, included all the code here, but could anyone, please, point me to the right direction?

What I actually want to achieve, is to collect the MeshFilters from the MeshColliders that a Raycast hits, and then retrieve them from another script.

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
1
Best Answer Wiki

Answer by entity476 · Jun 09, 2013 at 05:38 PM

I would like to provide it as an answer, since it solves my problem. The credits go to Eric, who helped me sort it out. So the lines I used:

I created a Public Variable to store my List:

 var unappliedMeshes = new List.<MeshFilter>();

I added items:

 unappliedMeshes.Add(unappliedMesh); //'unappliedMesh" is a MeshFilter retrieved from a mesh object.

And finally from another script, I call it as usually:

 var theScriptWithMyList : ListScript; // supposedly that the script, in which the List is created, is called ListScript.
 var meshFilters = theScriptWithMyList.unappliedMeshes
 Debug.Log (meshFilters[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
avatar image
0

Answer by InfiniBuzz · Jun 09, 2013 at 03:56 PM

Hi

My js knowledge is not the best since I use C# but lets try to help out.

 var unappliedMeshes = new Array(MeshFilter);

with this you try to create an array that has the MeshFilter class as an object at index 0.

if you know how many unappliedMeshes you will have (say 10) you can replace the line with

 var unappliedMeshes = new Array(10);

and then add MeshFilters with the Array's Push()-Method.

if you want to init the array with an existing mesh filter do

 var unappliedMeshes = new Array(unappliedMeshOne, unappliedMeshTwo);

Hope this helps and is what you are looking for.

Comment
Add comment · Show 1 · 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 entity476 · Jun 09, 2013 at 04:14 PM 0
Share

Thank you InfiniBuzz for your answer! Actually my problem has been introduced by trying to collect the unknown amount of $$anonymous$$eshFilters in 'one' script and then try to loop through them in some 'other' script. By only using the Array class (in order to use the Push command), I met "Implicit" warnings and I decided to convert the Array into a builtin array first. The problem came out at that line.

By the way, user Eric5h5, replying on my same query in the forums, he advised to not use Arrays but generic Lists, when we deal with only one type of items. I currently try to find the way to read the List's items I stored in the 'one' script from the 'other'.

I appreciate it, you took the time to reply.

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

15 People are following this question.

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

Related Questions

Compare indices in an array of Vector2s 0 Answers

Door Script Help! 0 Answers

Array:Implicit downcast from 'Object' to 'UnityEngine.AudioClip'. 1 Answer

is it better to use MeshFilter[] or three separate MeshFilter var's 1 Answer

transform array gives error. why? 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