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 TheMaster42 · Apr 25, 2013 at 06:38 PM · javascriptparticlesystemfor-loopkeywords

Unityscript - Using "as" to cast for GetComponentsInChildren returns empty array

I have an instantiated GameObject with multiple ParticleSystems on it (each on a GameObject that is a child of the main GameObject). To stop the entire thing from emitting further particles, I use this:

 function StopEmission(theParticleObject : GameObject) {
        var tempComponents : Component[];
     tempComponents = theParticleObject.GetComponentsInChildren(ParticleSystem);
     for (var particle : ParticleSystem in tempComponents) {
         particle.enableEmission = false;
     }
 }

However this generates an "implicit downcast" warning. I understand I should be able to cast out the error as follows:

     var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren(ParticleSystem) as ParticleSystem[];
     for (var particle : ParticleSystem in tempComponents) {
         particle.enableEmission = false;
     }

However, the for loop in this case never returns any results and I'm not sure why. I believe "`as`" will return null when the casting fails, but why shouldn't this work?

EDIT: This doesn't work either, harrumph:

     var tempComponents : Component[];
     tempComponents = theParticleObject.GetComponentsInChildren(ParticleSystem);
     for (var particle : ParticleSystem in tempComponents as ParticleSystem) {
         particle.enableEmission = false;
     }
Comment
Add comment · Show 6
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 AlucardJay · Apr 25, 2013 at 07:14 PM 0
Share

Not sure if this will help as I've never done anything like this.

The first example looks fine except for

 var tempComponents : Component[];

You should start by using the first example, but change that line to

 var tempComponents : ParticleSystem[];

When using GetComponent in uJS you can declare the parameter as a string or as a component type

 theParticleObject.GetComponentsInChildren( ParticleSystem );
 theParticleObject.GetComponentsInChildren( "ParticleSystem" ) as ParticleSystem;

you only need to append the as when you are using the string format.

For your second example, I think it is falling down because of the typecasting you use

  var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren(ParticleSystem) as ParticleSystem[];

should be

  var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren(ParticleSystem) as ParticleSystem;

though I could be very wrong about that!

You are populating an array of ParticleSystems, but each component is just a ParticleSystem.

Again the same rule applies : if you are not declaring the parameter as a string then you don't need to use as :

  var tempComponents : ParticleSystem[] = theParticleObject.GetComponentsInChildren( ParticleSystem );

Finally you can remove any downcast warnings by using

 #pragma downcast

but personally I never do this myself, it just masks the issues you have, I really don't know if pragma downcast fixes the problems on building, but in C# you definitely don't get away with incorrect typecasting =]

avatar image Eric5h5 · Apr 25, 2013 at 07:52 PM 0
Share

Downcasting isn't a problem, it's just a warning that you might not have intended it.

avatar image AlucardJay · Apr 25, 2013 at 07:54 PM 0
Share

Aah, thankyou, that is great information. I was holding my breath after your answer, wondering if and what I got wrong.

avatar image Eric5h5 · Apr 25, 2013 at 07:56 PM 1
Share

Well, also don't use strings if you can avoid it. ;)

avatar image Eric5h5 · Apr 25, 2013 at 08:02 PM 0
Share

Oh, and you would want to type as ParticleSystem[] rather than ParticleSystem (or rather, you would if that worked); it's an array, so trying to cast an array as a not-array would result in an error.

Also, the casting doesn't really have anything to do with strings; GetComponentsInChildren() always returns Component[] regardless. So the casting doesn't work for whatever reason. That's why it's better to use GetComponentsInChildren.(), since that does cast as an array of the correct type.

Show more comments

1 Reply

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

Answer by Eric5h5 · Apr 25, 2013 at 07:48 PM

Use the generic version of GetComponentsInChildren; that way the array is automatically cast to ParticleSystem:

 function StopEmission (theParticleObject : GameObject) {
     var tempComponents = theParticleObject.GetComponentsInChildren.<ParticleSystem>();
     for (var particle in tempComponents) {
        particle.enableEmission = false;
     }
 }
Comment
Add comment · Show 2 · 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 TheMaster42 · Apr 25, 2013 at 08:14 PM 0
Share

Bingo! That did it; thanks Eric. Why does GetComponentsInChildren(ParticleSystem) exist if only GetComponentsInChildren.ParticleSystem>(); works "properly?"

avatar image Eric5h5 · Apr 25, 2013 at 08:20 PM 0
Share

GetComponentsInChildren.() was added later. The non-generic version does work properly, just not conveniently. ;) (I rarely ever want to deal with Component.)

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

14 People are following this question.

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

Related Questions

Finding nearest object with a certain tag without for loops. 1 Answer

How to make a List Constructor? 1 Answer

Can I convert unityscript to boo? 0 Answers

have array target person of most priority/stick with top prior 0 Answers

Dynamically load UnityScript file 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