How to initialize ParticleEmitter created in code

I have created a particle emitter in code and I cannot initialize it:

ParticleEmitter emitter = (ParticleEmitter)gameObject.AddComponent("EllipsoidParticleEmitter");
emitter.minSize = 0.5f;

I get an exception on the second line:

DivideByZeroException: Division by zero

I get this exception when I set any properties on the emitter.

`ParticleEmitter`s go into correct slot of the `GameObject` and can be accessed through `.particleEmitter` property:

gameObject.particleEmitter.minSize = 0.5f;

That doesn't explain why your fragment doesn't work as it should have.


Well, actually your code works for me. I put it in `Start()`.

have you remembered to initialize the "gameObject" ?

try breaking up the problem into smallere bits.


Debug.Log(emitter) ... is this null or?

Debug.Log(gameObject) ... ?

or perhaps you need more parameters to be initialized before activating it?