Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by maciekish · Feb 01, 2012 at 01:36 PM · gameobjectcomponentparticleemitterinvalidcastexceptiongetcomponentsinchildren

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maciekish · Feb 01, 2012 at 02:20 PM 0
Share

"look at what GetComponentsInChildren() returns in debug mode" do you mean by Debug.Log or is there a different way to do that?

avatar image senad · Feb 01, 2012 at 02:24 PM 0
Share

I mean with setting a breakpoint and inspecting your variables.

Look at this: http://unity3d.com/support/documentation/$$anonymous$$anual/Debugger.html

avatar image maciekish · Feb 01, 2012 at 03:20 PM 0
Share

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?

avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maciekish · Feb 02, 2012 at 02:28 PM 0
Share

It's helped a lot, thanks!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges