x


: 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");
    }
}
more ▼

asked Mar 17 '12 at 01:40 PM

Mariol gravatar image

Mariol
19 2 4 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Mar 17 '12 at 02:23 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

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. Much appreciated :)

Mar 18 '12 at 07:58 PM Mariol
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1259
x391
x69

asked: Mar 17 '12 at 01:40 PM

Seen: 882 times

Last Updated: Mar 19 '12 at 11:56 AM