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
1
Question by Essential · Jan 20, 2014 at 06:32 PM · texturearraycolorgetpixelsinvalidcastexception

I can't see a reason for this Array.Reverse() error

I'm trying to reverse the array of pixel information, but the Reverse() command gives a runtime error that it "Cannot cast from source type to destination type." but they're both Color arrays! What's going on?

    var texture : Texture2D;
     
     function Start () {
         var test : Color[] = texture.GetPixels();
         var test2 : Color[] = test.Reverse();
     }
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

3 Replies

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

Answer by Essential · Jan 20, 2014 at 07:48 PM

WOW. So after exhaustive testing I discovered Reverse() doesn't actually return an array, it returns an IENumerable! Crazy! It's fixed by converting to an array after reversing…

 var test2 = test.Reverse().ToArray();
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
1

Answer by JoaoOliveira · Jan 20, 2014 at 06:41 PM

Reverse returns void, the change is applied directly to the array (in this case, "test").

If you don't need "test" for anything else, you can simply replace line 5 with:

 test.Reverse();

If you do need it, you should make a copy of "test" before applying the Reverse method.

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 Essential · Jan 20, 2014 at 07:05 PM 0
Share

Thanks for the reply but this doesn't work from what I can see. Just adding it onto the end doesn't reverse it, however it does return a reversed array. It works in the below int array example, but not for the Color array… Unless I'm missing something?

     var test = [0,1,2,3,4];
     var test2 = test.Reverse();
     for (var x in test2)
         Debug.Log( x );
avatar image
1

Answer by Uncasid · Jan 20, 2014 at 06:48 PM

I think you are supposed to use it like this:

 System.Array.Reverse(test);

I don't think you need to assign it to another variable.

Comment
Add comment · Show 3 · 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 JoaoOliveira · Jan 20, 2014 at 06:52 PM 0
Share

That is for C# Arrays.

I believe @Essential is using the Javascript-only one.

avatar image JoaoOliveira · Jan 20, 2014 at 06:58 PM 0
Share

Yes I wasn't questioning that, "C# Arrays" (maybe there is a better name for them...) can be used when coding in C# or Javascript in Unity.

What I meant is that @Essential seems to be using the Javascript version, as indicated by the use of "test.Reverse()" (which would be a compilation error using "C# arrays").

avatar image Uncasid · Jan 20, 2014 at 06:59 PM 0
Share

Yeah, I realized that, which is why I removed my comment, haha

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

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

Related Questions

How to access multiple components of a component at once? 2 Answers

Get color coordinate from the texture and convert to world space 1 Answer

Does enabling Read/Write on a texture double its memory usage? 1 Answer

Cycling Texture Array 1 Answer

Color Array in C# 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