Search Unity

How to Get ParticleSystem to Emit

Discussion in 'Editor & General Support' started by John-B, Nov 22, 2019.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    I have an app that used the old particle system that I'm trying to update, and Emit doesn't seem to work as it did in the old system. I use Emit to generate a fixed number of particles, then get the particle array and position the particles on the surface of a sphere. The new particle system doesn't seem to emit any particles, at least not any I can see.

    Code (CSharp):
    1. ParticleSystem objectDot;
    2. objectDot.Play();  // don't know if this is necessary
    3. objectDot.Emit(gStimNumDots); // emit the particles
    4.  
    5. ParticleSystem.Particle[] dotParticles;
    6. var numParticlesAlive = objectDot.GetParticles(dotParticles);  // get the array of particles
    7. print(numParticlesAlive);  // always zero
    8.  
    9. for (i = 0; i < numParticlesAlive; i++) {
    10.    dotParticles[i].position = objectArray[i];
    11. }
    12. objectDot.SetParticles(dotParticles, numParticlesAlive);
    It always shows zero particles generated. Looping and Play on Awake are turned off, maxParticles is above the maximum number of possible particles. Are there any other settings necessary for the particle system to emit on command? Is there any other possible reason the new particle system is not working the way the old system did?

    Also, is there a way to give particles an infinite lifetime in this new system?

    I just discovered that if I put the Emit call in Start, it generates the particles (so at least I know I have the particle parameters set correctly), but even so, GetParticles still returns zero and the array is empty. Anywhere else in the code, and no particles get generated (Inspector also shows zero particles).
     
    Last edited: Nov 23, 2019
  2. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    It looks like the problem was that I didn't initialize the particle array. I thought GetParticles works like GetPixels, which returns an array that's been resized to fit the data. However, with GetParticles, you have to initialize the array with enough elements to hold the emitted particles. Looks like Emit will only emit as many particles as there is room in the array. That's why when I just declared the array without giving it a size, it always emitted zero particles.