I can’t make the fire start in chapter 8 of “Unity 3.x Game Development Essentials”. During runtime the following error is thrown:
InvalidCastException: Cannot cast from source type to destination type.
Inventory.LightFire (UnityEngine.GameObject campfire) (at Assets/Scripts/Inventory.js:66)
Inventory.OnControllerColliderHit (UnityEngine.ControllerColliderHit col) (at Assets/Scripts/Inventory.js:57)
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)
Here is the relevant code:
function OnControllerColliderHit(col:ControllerColliderHit) {
if(col.gameObject.name == "campfire") {
Debug.Log(col.gameObject.GetType());
if(haveMatches) {
LightFire(col.gameObject);
} else {
textHints.SendMessage("ShowHint", "I could use this campfire to signal for help..
If only i could light it.");
}
}
}
function LightFire(campfire:GameObject) {
var fireEmitters:ParticleEmitter[];
fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter); //Exception here
for (var emitter:ParticleEmitter in fireEmitters) {
emitter.emit = true;
}
campfire.audio.Play();
Destroy(matchGUI);
haveMatches = false;
}