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

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.

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.

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 =/