Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 JoJoJet · Jul 13, 2015 at 12:38 PM · c#scripting problemerrorcompilercast

C# Getting "Cannot cast from source type to destination type" even though it's working.

Relevant part of the script:

 public object[] pictures;
 public int counter = 1;

 void  Update (){
 GetComponent<SpriteRenderer>().sprite = (Sprite)pictures[counter];
 }

This code works as it should, it changes the sprite of my GameObject to the desired object in the pictures[] array, the script works fine. But my only problem is that every frame the compiler gives me the "Cannot cast from source type to destination type" error, even though it works as it should. I would ignore this, but I'm pretty sure that when I try to build the game it wouldn't let me until I fix this error.

What is wrong, and how do I fix it?

Comment
Add comment · Show 5
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 DiegoSLTS · Jul 13, 2015 at 12:58 PM 0
Share

What's inside the pictures array? That's also relevant.

avatar image Notter · Jul 13, 2015 at 01:12 PM 0
Share

I don't understand why are you using an object[] for pictures, and not a Sprite[]. And if it's necessary, at least make it a safer cast, by checking that the object is of type sprite before casting.

exmaple:

 if (pictures[counter] is Sprite) 
 GetComponent().sprite = pictures[counter] as Sprite;
avatar image JoJoJet · Jul 13, 2015 at 01:33 PM 0
Share

I'm using object[] because I loaded the pictures out of a folder in Resources

avatar image JoJoJet · Jul 13, 2015 at 01:40 PM 0
Share

Thank you, Notter! That worked perfectly. Not quite sure why, though since it wouldn't have actually changed anything since everything in pictures[] was a sprite. oh well ¯_(ツ)_/¯

avatar image Notter · Jul 13, 2015 at 05:13 PM 0
Share

Well, it might be that sometimes in the pictures array you actually didn't have a sprite. i can't see the rest of the code so i don't know for sure. but since it happens in Update(), it means every frame you change the sprite, so it might be for like 1 frame it doesn't work, and in the next it does, so you don't notice the frame the gives the error.

just a theory though.

1 Reply

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

Answer by DiegoSLTS · Jul 13, 2015 at 04:54 PM

You said in the other comments that you're using an object array because you're loading the Sprites from Resources. Resources.Load and Resource.LoadAll both have a generic version where you can specify the type at compile time, and get references of that type only.

 Sprite[] pictures = Resources.LoadAll<Sprite>(path);

You should use that instead of casts at runtime, it's a lot safer.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Float to int casting in C# script with modulo giving division by zero error 1 Answer

MySql compiler error help~ (OSX) 0 Answers

Unity Follow backward 1 Answer

3 Days and I still can't figure out c# reflection, please help? Trying to use a protected Type and method. 1 Answer

The type or namespace name 'StateMachine' could not be found (are you missing a using directive or an assembly reference?) 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