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 /
This question was closed Sep 16, 2013 at 03:46 PM by Rabwin for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Rabwin · Aug 25, 2013 at 05:24 PM · arrayobjectcasting

Object to Foo is okay, but Object[] to Foo[] gives error.

Hey guys, this one is quite strange and I can't any info about this, or I'm searching for the wrong terms.

Basically I'm trying to build a reference manager that stores arrays of type Object[] (so that I can store multiple arrays of different types, of course not multiple types within one Object[])

Here is the code, and the error "InvalidCastException: Cannot cast from source type to destination type." occurs on the last line of the Start function, but the middle one is okay.

 public class Foo : MonoBehaviour
 {
     private void Start()
     {
         //create a one-length array containing myself.
         UnityEngine.Object[] test = new UnityEngine.Object[] { this };
         Foo fooTest = (Foo)test[0]; //casting one object is okay.
         Foo[] fooArray = (Foo[])test; //casting Object array as Foo array is bad.
     }
 }

I'd like to note that using the following doesn't give an error, but the array is still null.

 Foo[] fooArray = test as Foo[];
Comment
Add comment · Show 3
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 Rabwin · Aug 25, 2013 at 05:35 PM 0
Share

The only way around this that I can think of, is to create a Foo array at the size of the Object array, and loop through each element casting them every time (While this method works, I feel like it's a ridiculous work-around).

avatar image Jamora · Aug 25, 2013 at 05:46 PM 0
Share

If you're sure that each Object in test is a Foo, why not create a Foo array from the beginning?

avatar image Hoeloe · Aug 25, 2013 at 09:55 PM 1
Share

I feel I should add here that the "as" semantics (such as`test as Foo[]`) is defined to be a cast that returns null if the cast fails, rather than throwing an error.

1 Reply

  • Sort: 
avatar image
4
Best Answer

Answer by fafase · Aug 25, 2013 at 05:46 PM

Try using Linq:

 Foo[] fooArray = Array.ConvertAll(test, item => (Foo)item);

you will have to add using System; and using System.Linq;

Comment
Add comment · Show 7 · 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 Rabwin · Aug 25, 2013 at 05:54 PM 0
Share

Works nicely, and much better than writing a loop. Thanks fafase.

avatar image Hoeloe · Aug 25, 2013 at 09:56 PM 0
Share

Essentially, the Linq result does just loop through and copy the array, but it does so in a shorter line of code, and possibly one that is optimised at the bytecode level, making it potentially faster than writing the same procedure in the native language.

avatar image fafase · Aug 26, 2013 at 04:45 AM 0
Share

Actually Linq is slower, that is why it should not be used in Update. http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html This is probably due to the added code in the Linq version to be abe to work on any case while your own foreach loop is doing only what you want to.

avatar image Hoeloe · Aug 26, 2013 at 09:44 AM 0
Share

Probably true, actually. Linq does have to deal with a more general case (for example, in this case, invoking a function to do the cast, rather than doing it directly), which will hamper performance slightly. There are some Linq functions, though, which are optimised at bytecode level, though they tend to be the simpler ones, which don't need extra arguments to specify your specific case (i.e. ones that don't require invoking a delegate function).

avatar image Rabwin · Aug 28, 2013 at 01:35 PM 0
Share

Thanks for the specification/clear up. I guess I'll write my own (static?) for-loop to deal with this specific case.

Show more comments

Follow this Question

Answers Answers and Comments

17 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 avatar image avatar image

Related Questions

Array of GameObjects 4 Answers

Removing objects from an array 2 Answers

array of objects 1 Answer

Randomly instantiate objects from array without choosing the same item twice. 2 Answers

Instantiate duplicates script in subsequent Objects 2 Answers


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