- Home /
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
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.
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
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.
Update your initial question with the updated code, and point to where the error is now. Is it at the same line of code?
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 =/
Your answer
![](https://koobas.hobune.stream/wayback/20220613082612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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