- Home /
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!
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();
BTW. Found that code snippet here.. http://lmgtfy.com/?q=unity3d+resources+loadall+textures
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!