x


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

more ▼

asked Dec 24 '10 at 06:39 AM

Jason Hamilton gravatar image

Jason Hamilton
445 68 73 80

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Try casting the component you're referencing:

var Ditched : GemScriptV2 = gameObject.GetComponentsInChildren(GemScriptV2) as GemScriptV2;
more ▼

answered Dec 24 '10 at 10:35 PM

azzogat gravatar image

azzogat
918 3 16

You cannot cast Component[] to GemScriptV2. It's not going to return an error because of the as operator but Ditched will be null.

Feb 02 '11 at 12:27 PM Senhor de todo o Mal
(comments are locked)
10|3000 characters needed characters left

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;
    }
}
more ▼

answered Feb 02 '11 at 12:25 PM

Senhor de todo o Mal gravatar image

Senhor de todo o Mal
718 4 7 21

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5095
x3465
x1951
x37

asked: Dec 24 '10 at 06:39 AM

Seen: 2775 times

Last Updated: Dec 24 '10 at 06:39 AM