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 ShadowAngel · Oct 19, 2012 at 12:15 PM · invalidcastexception

Cannot cast from source type to destination type.

I relaize thet i did something wrong with var types but i cant find where...

 var QuestDB : System.Collections.Generic.List.<Quest> = new System.Collections.Generic.List.<Quest>();
 var ActiveQuests : System.Collections.Generic.List.<Quest> = new System.Collections.Generic.List.<Quest>();
 
 function Start ()
 {
 QuestInt();
 QuestAddActive("Test Quest");
 }
 function QuestInt ()
 {
 QuestDB.Add(TestQuest());
 }
 class Quest
 {
     var Name : String;
     var Description : String;
     var TimeOnTimer : float = 0.0;
     var TimerOnPause : boolean = true;
     var QuestTargetObject : GameObject = null;
     var QuestTargetCoords : Vector3 = new Vector3(0,0,0);
     var Stage : boolean[] = new boolean[10];
     
     function Quest(name : String, description : String, timeOnTimer : float, timerOnPause : boolean, questTargetObject : GameObject, questTargetCoords : Vector3)
     {
         Name = name;
         Description = description;
         TimeOnTimer = timeOnTimer;
         TimerOnPause = timerOnPause;
         QuestTargetObject = questTargetObject;
         QuestTargetCoords = questTargetCoords;
     }
     function Quest (name : String, description : String)
     {
         Name = name;
         Description = description;    
     }
 }
 function TestQuest() : Quest
 {
     var Name : String = "Test Quest";
     var Description : String = "TestTest";
     return new Quest(Name, Description);
 }
 function QuestAddActive(name : String)
 {
     for (var qst : Quest in QuestDB) // <-----Error here!!!!!
     {
         if (qst.Name != name)
             continue;
         Debug.Log(qst.Name);
         var Name : String = qst.Name;
         var Description : String = qst.Description;
         var TimeOnTimer : float = qst.TimeOnTimer;
         var TimerOnPause : boolean = qst.TimerOnPause;
         var QuestTargetObject : GameObject = qst.QuestTargetObject;
         var QuestTargetCoords : Vector3 = qst.QuestTargetCoords;
         ActiveQuests.Add(new Quest (Name, Description, TimeOnTimer, TimerOnPause, QuestTargetObject, QuestTargetCoords));
     }
 }

Ok, Updated code with List. Same error on for (var qst : Quest in QuestDB) in function QuestAddActive

Comment
Add comment · Show 1
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 whydoidoit · Oct 19, 2012 at 12:17 PM 0
Share

On which line does the error occur?

3 Replies

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

Answer by CHPedersen · Oct 19, 2012 at 12:53 PM

This kind of error happens because there is an item in the ArrayList QuestDB which cannot be cast to an object of type Quest, because it isn't of that type. Generally, it's not a good idea to use ArrayList at all. It's a remnant from .Net 2.0 which is probably only kept in the API for backwards compatibility reasons.

The problem is that ArrayList is actually a list of objects simply of type "object", i.e. the base class for all objects in .Net. That's why you can stick anything in it. But when you work with it, you have to box and unbox objects going in and out of the list to retrieve the original type, and because the list accepts anything, you can add objects of all different types, and they can't be cast to a single type when a loop tries to retrieve them. Use a generic List instead (System.Collections.Generic.List). You can define the type you want to store in a generic List (Quest, in your case), and the list is then typesafe from then on.

Theory aside, I think the root of your problem occurs here:

 function QuestInt ()
 {
     QuestDB.Add(TestQuest);
 }

Where you attempt to call the function TestQuest which returns a new test Quest object. But parameterless functions in javascript are called with empty parentheses after them, like so:

 QuestDB.Add(TestQuest());

I'm not a javascript programmer (I use C#) so I don't know why it event compiles without those parentheses. But it sure isn't adding a Quest object to your collection, and that's why the loop can't cast it to a Quest object.

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 Vice_Versa · Jun 26, 2017 at 11:09 PM 0
Share

Im curious as to how an ArrayList is "not a good idea" and "probably only kept for compatibility reasons" Arrays cannot be resized, the objects have to be the same type, ArrayLists are almost always more useful, and while it is a pain that they have to be casted, i dont think theyre outdated, though if theres a newer more superior alternative id love to hear it

avatar image
0

Answer by ShadowAngel · Oct 19, 2012 at 01:15 PM

thx for pointing me out to QuestDB.Add(TestQuest()); it is error. but i still have same error even after changing QuestDB to:

 var QuestDB : System.Collections.Generic.List.<Quest> = new System.Collections.Generic.List.<Quest>();

it still says InvalidCastException: Cannot cast from source type to destination type.

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 CHPedersen · Oct 19, 2012 at 01:23 PM 0
Share

Update your initial question with the updated code, and point to where the error is now. Is it at the same line of code?

avatar image ShadowAngel · Oct 19, 2012 at 01:43 PM 0
Share

ok code updated.

avatar image
0

Answer by ShadowAngel · Oct 19, 2012 at 01:54 PM

Ok NOW im realy at loss... I started to check all types in script. I added Debug.Log(QuestDB[0].GetType()); to quest init and i did get Quest type and error is gone... i dont get it. =( But wait theres more... I did used ArrayList again just to see if error gets back, and gues what... It didnot!!! Someone can explain to me whats going on? Only thing im geting to my mind is thet unity did not updated its log after i fixed QuestDB.Add(TestQuest()); error =/

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error on HSController.js 1 Answer

InvalidCastException when using Instantiate 1 Answer

Sprite Root Cannot cast from source type to destination type. 0 Answers

Sytem.InvalidCastException on Windows Phone 1 Answer

Door Script Help! 0 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