x


InvalidCastException: Cannot cast from source type to destination type.

full error
InvalidCastException: Cannot cast from source type to destination type.
Inventory.LightFire (UnityEngine.GameObject campfire) (at Assets/Scripts/Inventory.js:62)
Inventory.OnControllerColliderHit (UnityEngine.ControllerColliderHit col) (at Assets/Scripts/Inventory.js:53)
UnityEngine.CharacterController:Move(Vector3)
CharacterMotor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:229)
CharacterMotor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:331)

    function OnControllerColliderHit(col : ControllerColliderHit){
    if(col.gameObject.name == "campfire"){
       if(haveMatches && !fireLit){
         LightFire(col.gameObject);
       }else if(!haveMatches && !fireLit){
       textHints.SendMessage("ShowHint", "I could use this campfire to signal for help... \n if only i could light it..");
       }
    }
}

function LightFire(campfire : GameObject){
    var fireEmitters : ParticleEmitter[];
    fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter);
    for(var emitter : ParticleEmitter in fireEmitters){
       emitter.emit = true;
    }
    campfire.audio.Play();
    Destroy(matchGUI);
    haveMatches = false;
    fireLit = true;
}

any ideas

decided to try to build it and it gave me this warning

Assets/Scripts/Inventory.js(62,56): BCW0028: WARNING: Implicit downcast from 'UnityEngine.Component[]' to 'UnityEngine.ParticleEmitter[]'.

more ▼

asked Mar 22 '12 at 02:05 AM

NiteFlamesInc gravatar image

NiteFlamesInc
42 3 7 10

One idea: hit the 'edit' link under your question, select the code parts, hit the 010/101 button above the text area, to format your code.

Mar 22 '12 at 02:06 AM DaveA

fixed sry bout that

Mar 22 '12 at 02:29 AM NiteFlamesInc

Thanks,helps a bit. Which is line 62?

Mar 22 '12 at 02:35 AM DaveA
fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter); is line 62
Mar 22 '12 at 02:40 AM NiteFlamesInc
(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

var fireEmitters : ParticleEmitter[];

var fireEmitters : Component[];

more ▼

answered Mar 27 '12 at 07:33 PM

NiteFlamesInc gravatar image

NiteFlamesInc
42 3 7 10

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

This looks suspect:

fireEmitters = campfire.gameObject.GetComponentsInChildren(ParticleEmitter)

campfire is already a GameObject, so you should omit the .gameObject part of that line.

more ▼

answered Mar 22 '12 at 02:11 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

took that part out same error

Mar 22 '12 at 02:29 AM NiteFlamesInc

Which is line 62? It looks pretty ok. Sometimes putting 'as ParticleEmitter[]' after the GetComponents... call helps.

Mar 22 '12 at 02:38 AM DaveA

NullReferenceException: Object reference not set to an instance of an object Inventory.LightFire (UnityEngine.GameObject campfire) (at Assets/Scripts/Inventory.js:63) Inventory.OnControllerColliderHit (UnityEngine.ControllerColliderHit col) (at Assets/Scripts/Inventory.js:53) UnityEngine.CharacterController:Move(Vector3) CharacterMotor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:229) CharacterMotor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:331)

thats what i get for putting in 'fireEmitters = campfire.GetComponentsInChildren as ParticleEmitter[];'

Mar 22 '12 at 02:54 AM NiteFlamesInc
(comments are locked)
10|3000 characters needed characters left

I'm not 100% sure, but try:

fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter) as ParticleEmitter[];
more ▼

answered Mar 27 '12 at 07:39 PM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

(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:

x1950
x37

asked: Mar 22 '12 at 02:05 AM

Seen: 1761 times

Last Updated: Mar 27 '12 at 07:39 PM