- Home /
Question by
Jason Hamilton · Dec 24, 2010 at 06:39 AM ·
javascripterrorinvalidcastexception
InvalidCastException: Cannot cast from source type to destination type. ???
hey, i have no idea what is causing this;
function Update ()
{
if(Input.GetButtonDown("Jump") && isShooter)
{
transform.DetachChildren();
isShooter = false;
var Ditched : GemScriptV2 = gameObject.GetComponentsInChildren(GemScriptV2);
Ditched.ditched = true;
}
}
the var ditched line is the line where the error is coming from :S
Comment
Answer by azzogat · Dec 24, 2010 at 10:35 PM
Try casting the component you're referencing:
var Ditched : GemScriptV2 = gameObject.GetComponentsInChildren(GemScriptV2) as GemScriptV2;
You cannot cast Component[] to GemScriptV2. It's not going to return an error because of the as operator but Ditched will be null.
Answer by Senhor de todo o Mal · Feb 02, 2011 at 12:25 PM
var Ditched is of type GemScriptV2.
gameObject.GetComponentsInChildren(GemScriptV2) returns an array of Component.
You cannot cast Component[] to GemScriptV2.
Try this:
function Update ()
{
if(Input.GetButtonDown("Jump") && isShooter)
{
transform.DetachChildren();
isShooter = false;
var Ditched : GemScriptV2 = gameObject.GetComponentInChildren.<GemScriptV2>();
Ditched.ditched = true;
}
}