In my game, you are supposed to light a fire (turn emit on for two particle emitters, smoke and fire). The game runs fine and I am not prevented from playing, but when I get to the point where I “collide” with the fire in order to start it, I get the following error:
“InvalidCastException: Cannot cast from source type to destination type.
Inventory.LightFire (UnityEngine.GameObject campfire) (at Assets/Scripts/Inventory.js:76)”
After those two lines, it also reads this:
"Inventory.OnControllerColliderHit (UnityEngine.ControllerColliderHit col) (at Assets/Scripts/Inventory.js:59)
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)
"
It seems to say that in my Inventory script on line 76 there is a problem, but despite googling this error and reading on here, I cant figure out what I am doing wrong. I struggled with the settings of the particle emitters anyway, so maybe its something wrong there? If someone wouldnt mind looking through my script and seeing if something is wrong in it that would be greatly helpful! ( have noted which line is line 59 and line 76, I hope that helps.)
This is the script entitled: inventory (per the error)
#pragma strict
static var charge : int = 0;
var collectSound : AudioClip;
//HUD
var hudCharge : Texture2D[];
var chargeHudGUI : GUITexture;
//Generator
var meterCharge : Texture2D[];
var meter : Renderer;
//matches
private var haveMatches : boolean = false;
var matchGUIprefab : GUITexture;
private var matchGUI : GUITexture;
var textHints : GUIText;
private var fireBurning : boolean = false;
//Win the game
var winObj : GameObject;
function Start () {
charge = 0;
}
function Update () {
}
function CellPickup()
{
HUDon();
AudioSource.PlayClipAtPoint(collectSound, transform.position);
charge++;
chargeHudGUI.texture = hudCharge[charge];
meter.material.mainTexture = meterCharge[charge];
}
function HUDon()
{
if(!chargeHudGUI.enabled)
{
chargeHudGUI.enabled = true;
}
}
function MatchPickup()
{
haveMatches = true;
AudioSource.PlayClipAtPoint(collectSound,transform.position);
var matchHUD : GUITexture = Instantiate(matchGUIprefab, Vector3(0.15,0.1,0),transform.rotation);
matchGUI = matchHUD;
}
function OnControllerColliderHit(col:ControllerColliderHit)
{
if(col.gameObject.name == "campfire")
{
if(haveMatches && !fireBurning)
{
LightFire(col.gameObject); **(this is line 59)**
}
else if(!haveMatches && fireBurning)
{
textHints.SendMessage("ShowHint", "Ouch that is hot!");
}
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); **(This is line 76)**
for(var emitter: ParticleEmitter in fireEmitters)
{
emitter.emit = true;
}
campfire.audio.Play();
Destroy(matchGUI);
haveMatches=false;
winObj.SendMessage("GameOver");
}