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 MakeITeasy · Dec 05, 2013 at 05:12 PM · javascriptarraytexturescastinginvalidcastexception

InvalidCastException error and logic error

Hi everyone! Project: i have 6 main objects (6 parents and 6 children) and every time my game starts they take random textures. See below a part of my js code:

 function Start () {
 var texturesCollection: Object[] = Resources.LoadAll("Front_Face_cards");
 var length_ = texturesCollection.length -2;
 var range_1 = Random.Range(0,length_);
 
 var bckgndTxtr_Collection: Object[] = Resources.LoadAll("Back_Face_cards");
 var bckgndTxtr_length_ = bckgndTxtr_Collection.length;
 var bckgndTxtr_range_ = Random.Range(0,bckgndTxtr_length_);

 //all objects (plane objects) get a texture 
         GameObject.Find("card_1").renderer.material.mainTexture = bckgndTxtr_Collection[bckgndTxtr_range_]; //<- here is the error (line 14)
         GameObject.Find("card_2").renderer.material.mainTexture = bckgndTxtr_Collection[bckgndTxtr_range_];
         GameObject.Find("card_3").renderer.material.mainTexture = bckgndTxtr_Collection[bckgndTxtr_range_];
         GameObject.Find("card_4").renderer.material.mainTexture = bckgndTxtr_Collection[bckgndTxtr_range_];
         GameObject.Find("card_5").renderer.material.mainTexture = bckgndTxtr_Collection[bckgndTxtr_range_];
         GameObject.Find("card_6").renderer.material.mainTexture = bckgndTxtr_Collection[bckgndTxtr_range_];
 
 //all objects (plane objects) get a texture, but the following plane objects are children to the ones above, for example "front_card_1" is a child to "card_1" etc.
 if(GameObject.Find("front_card_1") && GameObject.Find("front_card_2")){
     GameObject.Find("front_card_1").renderer.material.mainTexture = texturesCollection[range_1]; //<- here is the error (line 24)
     GameObject.Find("front_card_2").renderer.material.mainTexture = texturesCollection[range_1];
 }
 
 if(GameObject.Find("front_card_3") && GameObject.Find("front_card_6") ){
     GameObject.Find("front_card_3").renderer.material.mainTexture = texturesCollection[range_1+1];
     GameObject.Find("front_card_6").renderer.material.mainTexture = texturesCollection[range_1+1];
 }
 
 if(GameObject.Find("front_card_4") && GameObject.Find("front_card_5") ){
     GameObject.Find("front_card_4").renderer.material.mainTexture = texturesCollection[range_1+2];
     GameObject.Find("front_card_5").renderer.material.mainTexture = texturesCollection[range_1+2];
 }
 }

Problem: the thing is every time i play the scene sometimes i get this compile error saying: "InvalidCastException: Cannot cast from source type to destination type. StartUp.Start () (at Assets/Lvl1/StartUp.js:23)" and sometimes i get this one saying: "InvalidCastException: Cannot cast from source type to destination type. StartUp.Start () (at Assets/Lvl1/StartUp.js:14)" and some other times i don't get any error, which means that the scene works like a charm.

I ask for two things: 1)I don't understand the two above errors. 2)Do i have any other option to shrink somehow the above code, and more specifically, can i type a single line or two instead of the 6 lines i dedicate for my 6 card objects which they get the same texture?

Any help would be most welcome. Thank you in advance!

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

1 Reply

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

Answer by flaviusxvii · Dec 05, 2013 at 05:33 PM

InvalidCastException happens when you try to force one type of thing into a variable of incompatible type.

bckgndTxtr_Collection is type Object[].. and items in it would be Object

mainTexture is a Texture.. you can't just assign an Object to a Texture.

You could do something like this:

 using System.Linq;
 
   var textures = Resources.LoadAll("Whatever", typeof(Texture2D)).Cast<Texture2D>().ToArray();
Comment
Add comment · Show 2 · 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 flaviusxvii · Dec 05, 2013 at 05:34 PM 0
Share

BTW. Found that code snippet here.. http://lmgtfy.com/?q=unity3d+resources+loadall+textures

avatar image MakeITeasy · Dec 05, 2013 at 07:26 PM 0
Share

I fixed it. Thanx for ur answer @flaviusxvii. The answer to my problem was the casting...

What u suggested was on C#, because i am using js, the solution is: from line 11 to line 31 i use casting -> as Texture2D Thanx a lot again!

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

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

Casting Error C# between Object[] and Texture2D[] 2 Answers

Sytem.InvalidCastException on Windows Phone 1 Answer

expected. Insert a semicolon at the end. When the end that it says, is a } Could someone help us fix this? 2 Answers

Javascript - find a value in an array 0 Answers

Array Index out of range (JavaScript) 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