Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Mariol · Mar 17, 2012 at 01:40 PM · prefabtexturescast

: 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");
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mariol · Mar 18, 2012 at 07:58 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Premade Texture Use 1 Answer

How can I make that everyTime I change the texture of material, it won't change texture of every Instantiated Object that has that material 2 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges