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 Glark · Aug 18, 2012 at 02:32 AM · buttontoggletrailrenderergetcomponentsinchildren

My code works but produces an error in the console, not sure what's wrong but I would like to fix it.

Following the guide in the use of GetComponentsInChildren, and using the code below to have a toggle button that turns on and off trails on object(TrailRenderer). I get the ErrorInvalidCastException: Cannot cast from source type to destination type. I've looked up the error but can't make much sense of it from the examples I've found. I am happy the code works, the trails turn on and off easily! But I think the error means I'm doing something wrong. Thank you!

 var trailrend : TrailRenderer[];
 trailrend = gameObject.GetComponentsInChildren(TrailRenderer);
 var curt = false;
 
 function OnMouseDown() {
         
         if (!curt) {
         
         for (var rend : TrailRenderer in trailrend) {
     rend.enabled = true;
     }
         
         } else {
         
         for (var rend : TrailRenderer in trailrend) {
     rend.enabled = false;
     
     }
     
         }
         
         curt = !curt;
         
         }
         
     
         
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

1 Reply

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

Answer by CharlesD · Aug 18, 2012 at 07:52 AM

Ah well this isn't a biggy. What's happening is that GetComponentsInChildren returns an object of type "Component[]" despite what specific type of component you wanted it to find. So when Javascript attempts to store a variable of type "Component[]" into a variable specified as type "TrailRenderer[]" it realizes that the data types do not match and cannot be converted to one another automatically. So long story short all you have to do is change this...

 trailrend = gameObject.GetComponentsInChildren(TrailRenderer);

to this...

 trailrend = gameObject.GetComponentsInChildren(TrailRenderer) as TrailRenderer[];

The extra statement tells Javascript that you want to format GetComponentsInChildren's return value as an array of TrailRenderers rather than an array of Components. I have not tested this as I am on my phone, but given the error and my knowledge of the C# version of the method, I believe this should fix it.

Comment
Add comment · Show 4 · 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 Glark · Aug 18, 2012 at 10:52 AM 0
Share

Hey! Thanks for getting back to me. I added the changes you suggest and while it did make the console error disappear it actually disabled my button/Trails in a very odd way. So this is what happens: With your code above I can place my Trails into the variable array in the inspector, just as before, but then when starting the game it removes them immediately from that array, it basically clears it. On stopping the gane they are there again, so it's just when it's run. I have one or two ideas what it might be(the #Start or #Awake functions?) but other than that I don't know. Anyway, thanks for your help, I did of course back up my original code so no worries.

avatar image CharlesD · Aug 18, 2012 at 07:24 PM 0
Share

Hmmm... The code you showed above in your question actually does not need you to explicitly add the trail renderers to the array, it automatically searches the gameObject it is attached to and its children for all TrailRenderers and adds them to the array, rewriting whatever it is you assigned to it to begin with. What I assume to be happening is that there are no children of the gameObject you attached this script to (or the children do not have TrailTenderers) and therefore the method is returning an empty array. If you want to manually set which TrailTenderers are in the array then completely comment out the line I asked you to change. If you do want the script to set the array automatically then the gameObject you dragged the script onto or any one of its children need to have a TrailRenderer component attached. However I would personally recommend the first solution as it sounds more like what you wanted to begin with. Good Luck.

avatar image Glark · Aug 20, 2012 at 03:29 AM 0
Share

YES! That totally worked! Thanks so much for sticking through it with me Charles!

avatar image CharlesD · Aug 20, 2012 at 03:32 AM 0
Share

No problem, glad to hear its working now.

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

9 People are following this question.

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

Related Questions

Check GUI toggle button with the M keyboar Key 2 Answers

Is it a bad idea to add and remove listeners for toggles and buttons at runtime? 2 Answers

GUI window help 1 Answer

Toggle through sprites on UI button press 0 Answers

Whats wrong with my GUI.Toggle? 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