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
0
Question by Tommy · Oct 18, 2010 at 07:13 PM · errorcastsourceinvalidcastexceptiondestination

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

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · 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
0

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.

Comment
Add comment · 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
0

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?

Comment
Add comment · 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
0

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?

Comment
Add comment · Show 2 · 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 Cyb3rManiak · Oct 18, 2010 at 09:08 PM 0
Share

$$anonymous$$y bad... var player1: Texture2D; or var player1: Texture;

I can't remember how javascript will handle it, so try both.

avatar image Tommy · Oct 19, 2010 at 02:36 PM 0
Share

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

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

No one has followed this question yet.

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


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