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 lampshade · Jun 26, 2010 at 06:28 PM · instantiationemitparticleanimator

Invalid Cast Exception Error (on instantiation)

Hi,

When I try to do the following in my PlayerInstantiatedParticles C# file:

ParticleAnimator [] myEmissions;
// under certain condtions for the player instantiate the particles
myEmissions = (ParticleAnimator) Instantiate(ParticlePrefab, 
    transform.position, Quaternion.identity);

I get a run-time invalid cast exception error: Cannot cast from source type to destination type.

--This error leads me to believe that the FadeAndClearParticles function is not working. I believe that it would work if I could mitigate the invalid cast exception error..

(The instantiation works fine, I just can't see any change with the particles fading)

I could use a GameObject to prevent the invalid cast exception error, but I need to be able to access the colorAnimation member for fading particles out and then clearing them:

//after instantiation public static Color [] holder;

public static ParticleEmitter [] myEmitters;

public static IEnumerator FadeAndClearParticles() { holder = new Color[5]; holder[0].a = 1;

 while (holder[0].a > 0) {
     ParticleAnimator [] myEmits = new ParticleAnimator [] 
         { PlayerInstantiatedParticles.myEmissions };

     foreach (ParticleAnimator m_emitter in myEmits) {
         if (m_emitter) {                
             holder = m_emitter.colorAnimation;
             holder[0].a -= 0.005f;
             m_emitter.colorAnimation = holder;
         }
     }
 }
 foreach (ParticleAnimator m_emitter in emitters) {
     if (m_emitter) {
         m_emitter.particleEmitter.emit = false;
         m_emitter.particleEmitter.ClearParticles();
     }
 }

 yield return 0;

}

Comment
Add comment · Show 2
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 qJake · Jun 26, 2010 at 06:53 PM 0
Share

Please don't add HT$$anonymous$$L to your post unless you absolutely know how to use it. ;) There's a Code Format button in the toolbar for a reason.

avatar image Cyclops · Jun 26, 2010 at 06:56 PM 0
Share

@SpikeX, I just reformatted his code - I hope you're not doing same. :) And @lampshade, I think I preserved the code's meaning when I reformatted, you might double-check to be sure.

1 Reply

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

Answer by qJake · Jun 26, 2010 at 07:07 PM

Might I ask why you even have a function called "FadeAndClearParticles", when the particle animator can do that for you, without having to write any code? I may be wrong, but it looks like you're trying to reinvent the wheel here... just set one of the five color properties in the ParticleAnimator component on your game object (including the Alpha value if you're using a transparent particle shader), and then your particles will animate through each color automatically. The particles are also automatically cleaned up once they die. So instead of using this script, you could just set the five color properties on the ParticleAnimator to slowly fade out over time, something like this:

alt text

Notice the alpha values slowly fade out over time, going from 255 -> 0. This will do exactly what your script is trying to do (except it does it better).

You need to be using a Transparent shader for this to work, though. Unity comes with some good Transparent particle shaders.

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 lampshade · Jun 26, 2010 at 11:21 PM 0
Share

Based on certain conditions for the player, the particles will need to fade out, that is why I thought I needed to do it through code.

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

No one has followed this question yet.

Related Questions

How do i make a particle emmit a fire in unity 3.x? 1 Answer

Emit particles along a animated mesh 1 Answer

Instantiation happens three times, but called only once 1 Answer

Instantiation causing issues x,y 1 Answer

Cost of instantiation 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