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 NiteFlamesInc · Mar 22, 2012 at 02:05 AM · invalidcastexception

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[]'.

Comment
Add comment · Show 4
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 DaveA · Mar 22, 2012 at 02:06 AM 0
Share

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.

avatar image NiteFlamesInc · Mar 22, 2012 at 02:29 AM 0
Share

fixed sry bout that

avatar image DaveA · Mar 22, 2012 at 02:35 AM 0
Share

Thanks,helps a bit. Which is line 62?

avatar image NiteFlamesInc · Mar 22, 2012 at 02:40 AM 0
Share
 fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter); is line 62

3 Replies

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

Answer by NiteFlamesInc · Mar 27, 2012 at 07:33 PM

var fireEmitters : ParticleEmitter[];

var fireEmitters : Component[];

Comment
Add comment · 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
1

Answer by by0log1c · Mar 27, 2012 at 07:39 PM

I'm not 100% sure, but try:

 fireEmitters = campfire.GetComponentsInChildren(ParticleEmitter) as ParticleEmitter[];
Comment
Add comment · 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
0

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.

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 NiteFlamesInc · Mar 22, 2012 at 02:29 AM 0
Share

took that part out same error

avatar image DaveA · Mar 22, 2012 at 02:38 AM 1
Share

Which is line 62? It looks pretty ok. Sometimes putting 'as ParticleEmitter[]' after the GetComponents... call helps.

avatar image NiteFlamesInc · Mar 22, 2012 at 02:54 AM 0
Share

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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