- Home /
: Cannot cast from source type to destination type." when switching textures on a prefab
Hi,
I'm trying to switch the textures on 16 different prefabs that I instantiate at the same time. With the code below I have managed to do this. Unfortunately, I am now getting the warning message "InvalidCastException: Cannot cast from source type to destination type." setLetterVariables.Letter (System.Object points, System.Object id)". I'm quite new to Unity so I'm not sure what this means, however I would like to solve this warning before I move on. Can anyone tell me what this means and how to get rid of it?
Thanks
#pragma strict
var switchTime = 3;
var seconds = switchTime;
function Start () {
InvokeRepeating ("Countdown", 1.0, 1.0);
}
function Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
print("DEBUG: BRING UP IN GAME MENU");
}
}
function Countdown () {
seconds -= 1;
if (seconds == 0){
//print("Resetting");
seconds = switchTime;
Destroy(gameObject);
SwitchBlock();
}
}
function SwitchBlock(){
var texture = new Array ("gamepieceA",
"gamepieceB",
"gamepieceC",
"gamepieceD",
"gamepieceE",
"gamepieceF",
"gamepieceG",
"gamepieceH",
"gamepieceI",
"gamepieceJ",
"gamepieceK",
"gamepieceL",
"gamepieceM",
"gamepieceN",
"gamepieceO",
"gamepieceP",
"gamepieceQ",
"gamepieceR",
"gamepieceS",
"gamepieceT",
"gamepieceU",
"gamepieceV",
"gamepieceW",
"gamepieceX",
"gamepieceY",
"gamepieceZ");
var random_texture = Mathf.Floor(Random.Range(0,texture.length));
var new_letter : GameObject;
new_letter = Instantiate(Resources.Load("letter"), transform.position, transform.rotation);
switch(texture[random_texture]){
case "gamepieceA" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceB" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceC" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceD" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceE" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceF" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceG" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceH" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceI" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceJ" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceK" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceL" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceM" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceN" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceO" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceP" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceQ" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceR" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceS" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceT" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceU" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceV" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceW" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceX" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceY" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
case "gamepieceZ" :
new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture]);
break;
default :
print ("Got default");
}
}
Answer by aldonaletto · Mar 17, 2012 at 02:23 PM
This error means that you're trying to assign a value of some type to a variable of an incompatible type. You should have indicated in which line the error is occurring! Anyway, probably some object you're loading have the wrong type. You could add the type to each Load instruction, what could avoid or at least help to find which object is causing the error. Furthermore, you don't need that gigantic switch statement: it's a waste of time, since the code in each case is exactly the same. You should also declare the texture Array variable outside the function: variables declared inside a function are temporary - they are created when the function is called, and deleted upon return.
... // declare this variable outside any function: var texture = new Array ( "gamepieceA", "gamepieceB", "gamepieceC", "gamepieceD", "gamepieceE", "gamepieceF", "gamepieceG", "gamepieceH", "gamepieceI", "gamepieceJ", "gamepieceK", "gamepieceL", "gamepieceM", "gamepieceN", "gamepieceO", "gamepieceP", "gamepieceQ", "gamepieceR", "gamepieceS", "gamepieceT", "gamepieceU", "gamepieceV", "gamepieceW", "gamepieceX", "gamepieceY", "gamepieceZ" );
function SwitchBlock(){ // don't use Mathf.Floor - Random.Range does the job alone: var random_texture = Random.Range(0,texture.length);
var new_letter : GameObject; // declare the object type inside Load: new_letter = Instantiate(Resources.Load("letter", GameObject), transform.position, transform.rotation); // you don't need that huge switch - this instruction does exactly the same: new_letter.renderer.material.mainTexture = Resources.Load(texture[random_texture], Texture2D); }
Thanks. This has gotten rid of the warning. The switch statement was going to be used to set variables to each new prefab. I was going to use the texture to set the correct variables to each block.
Thanks for the help though. $$anonymous$$uch appreciated :)
Your answer
Follow this Question
Related Questions
Premade Texture Use 1 Answer
Changing texture on prefab at runtime (c#) 1 Answer
Collect all resources (dependencies) used in a certain prefab. 0 Answers
Pile of leaves 1 Answer