InvalidCastException: Cannot cast from source type to destination type. (8793)

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

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.

@ start of script i have `var player1 = guiTexture;`, which i have later on selected a GUI texture for. Won't work?

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".

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?