- Home /
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[]'.
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.
fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter); is line 62
Answer by NiteFlamesInc · Mar 27, 2012 at 07:33 PM
var fireEmitters : ParticleEmitter[];
var fireEmitters : Component[];
Answer by by0log1c · Mar 27, 2012 at 07:39 PM
I'm not 100% sure, but try:
fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter) as ParticleEmitter[];
Answer by DaveA · Mar 22, 2012 at 02:11 AM
This looks suspect:
fireEmitters = campfire.gameObject.GetComponentsInChildren(ParticleEmitter)
campfire is already a GameObject, so you should omit the .gameObject part of that line.
Which is line 62? It looks pretty ok. Sometimes putting 'as ParticleEmitter[]' after the GetComponents... call helps.
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:$$anonymous$$ove(Vector3) Character$$anonymous$$otor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.js:229) Character$$anonymous$$otor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Character$$anonymous$$otor.js:331)
thats what i get for putting in 'fireEmitters = campfire.GetComponentsInChildren as ParticleEmitter[];'
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
Getting XP Script Help 2 Answers