- Home /
InvalidCastException: Cannot cast from source type to destination type.
Hey developers! I get the error in the description (InvalidCastException: Cannot cast from source type to destination type.) while trying to run the following script
SNIP
case "Player1":
gameObject.guiTexture.enabled = true;
guiTexture.texture = player1; //Error here
break;
SNIP
Why?
Thanks! //Tommy
Answer by Cyb3rManiak · Oct 18, 2010 at 07:57 PM
As a rule, try to always define your variables in a strict manner by stating its type...
var player1: GuiTexture;
That way the error messages will make more sense, and the code will be much less prone to mistakes and run-time errors. In fact, I would recommend adding
#pragma strict
to the start of your javascript files to force the compiler to complain about issued that usually it will "let slide".
Answer by skovacs1 · Oct 18, 2010 at 07:18 PM
What's wrong is that player1
doesn't contain information for a Texture
type and/or has already been typed as something else. GUITexture.texture
is expecting an object of type Texture
and you are passing it a different type.
What is player1
? It is obviously not a Texture
. You will need to include more code than just this for anyone to conclusively tell you why it isn't a Texture.
Answer by Tommy · Oct 18, 2010 at 07:23 PM
@ start of script i have var player1 = guiTexture;
, which i have later on selected a GUI texture for. Won't work?
Answer by Tommy · Oct 18, 2010 at 08:06 PM
Okay, so i added #pragma strict
to the script, and i got this error instead:
BCE0022: Cannot convert 'UnityEngine.GUITexture' to 'UnityEngine.Texture'.
It dosn't make any more sence to me. If it can't convert it, how can i solve this?
$$anonymous$$y bad... var player1: Texture2D; or var player1: Texture;
I can't remember how javascript will handle it, so try both.
Legendary, it works fine. Still, i can only show i texture, i need it to be a GUI texture, since om using it as a popup menu for each character. Any ideas?
Your answer
Follow this Question
Related Questions
"Cannot cast from source type to destination type"- instantiating 4 Answers
InvalidCastException: Cannot cast from source type to destination type. 3 Answers
Cannot cast from source to destination type 1 Answer
Invalid Cast error when my format seems the same as the script reference example? 1 Answer
iTween-Error :: "Cannot cast from source type to destination type" 2 Answers