- Home /
InvalidCastException when trying to access child components
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..\nIf 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;
}
Answer by senad · Feb 01, 2012 at 02:18 PM
I do not see anything wrong in that code. Maybe you can look at what GetComponentsInChildren() returns in debug mode to get a hint at the problem.
For this purpose you can change the type of fireEmitters to Component[] to avoid the exception. (and change it back later)
"look at what GetComponentsInChildren() returns in debug mode" do you mean by Debug.Log or is there a different way to do that?
I mean with setting a breakpoint and inspecting your variables.
Look at this: http://unity3d.com/support/documentation/$$anonymous$$anual/Debugger.html
Still learning Unity, thanks :) And uhm.. Changing it to Component[] actually fixed the problem, the fire gets lit now :S
But just for the record, watching the array returns UnityEngine.ParticleEmitter, so i guess in theory it should work with my original code. Anyone knows why it doesnt?
Answer by willgoldstone · Feb 02, 2012 at 02:25 PM
Hi there, I cover how to correct this bug in my errata pages for the book, see the workaround there, but Senad is correct in his / her assumption that casting to generic Component solves the issue, see the site for any other mistakes in the book.
http://unitybook.net/book-errata/
Thanks for buying the book, I hope it helps you get started with Unity.
Your answer
Follow this Question
Related Questions
Disable/Enable Script or Add/Remove? 1 Answer
Disabling GameObject doesn't disable components 0 Answers
Is it OK to use public accessers for unique gameobjects (C#) ? 3 Answers
Find a Component/GameObject Using an Interface Reference 2 Answers
What is the difference between GameObject.GetComponent and Component.GetComponent? 1 Answer