- Home /
How to create ParticleEmitter in code
When I try to add a particle emitter in code like this:
ParticleEmitter emitter = gameObject.AddComponent<ParticleEmitter>();
I get this error:
Cannot add component of type 'ParticleEmitter' because it is abstract. Add component of type that is derived from 'ParticleEmitter' instead.
I looked through the documentation and I couldn't find any classes which derive from ParticleEmitter so I tried to make my own like this:
public class RocketExhaustEmitter : ParticleEmitter
Which gives me the following error:
error CS0509:
RocketExhaustEmitter': cannot derive from sealed type
UnityEngine.ParticleEmitter'
Is there a way to create a ParticleEmitter from scratch in code or do I have to use a Prefab?
Answer by femi · Dec 31, 2010 at 12:49 AM
ParticleEmitter emitter = (ParticleEmitter)gameObject.AddComponent("EllipsoidParticleEmitter");
or
ParticleEmitter emitter = (ParticleEmitter)gameObject.AddComponent("MeshParticleEmitter");
Update:
Since you can only have a variable of their parent type, ParticleEmitter, you can only access members they inherit from it, but nothing specific to the particular children types e.g. size of the spheroid or wether mesh emitter is systematic. But this is still useful if you want to create your own particle system - you would then have an instance of the ParticleEmitter with emit
property set to false
and emit and/or animate particles yourself accessing particles
array property.
Thanks, that worked but I had to add a cast to get it to compile. I now have a new problem, I will start a new question for it.
That's garbage that you have to use the string version for this. How did you figure that out?
Agreed, I think it's more of a bug than a feature, as you'll see from my follow up question.
Petroz, I added the cast to the answer, sorry about that. Jessy, I figured that out by trying :-)