1
\$\begingroup\$

I have a script that controls how the particles in a particle emitter should look. (XNA example here) Yet it doesn't seem to update correctly. With an orb that should have 50 particles only a few show up. I tried to debug it to see what was going on and it all started working again. I removed the debugging and I was back to only a few particles being visible.

I put in a simple debug message (Debug.Log("hi")) and it worked, but it was a little slow since I was putting out a debug message 50 times per frame.

I wanted to know if I wasn't getting the correct number of particles so I set the starting color to something with full alpha instead of 0 and all of the particles showed up even without the debug message, but they just pop in instead of fading now. This means that somehow it's not fading in correctly unless there's a debug statement. It makes no sense to me.

The issue seems to be in the "if (myParticles[i].ChangingColors)" section of Update.

Here are my start and update functions.

OrbBase - Handles the number of particles.
RotateParticleFinal - Holds the data that I want to set to the particle.

void Start () 
{
    myBase = gameObject.GetComponent<OrbBase>();

    emitter = (GameObject)Instantiate(ParticlePrefab);
    emitter.transform.parent = this.transform;
    emitter.transform.position = transform.position;    

    myParticles = new List<RotateParticleFinal>();
    myRemovedParticles = new List<RotateParticleFinal>();

    for (int i=0; i < myBase.Particles; i++)
    {
        float r  = GetNewRadius();
        myParticles.Add(new RotateParticleFinal(r, color));
    }
}

void Update () 
{   
    #region Deal with changing particle count
    while (myParticles.Count < myBase.Particles)
    {
        float r  = GetNewRadius();          
        myParticles.Add(new RotateParticleFinal(r, color));
    }

    for (int i=myParticles.Count-1; i >= 0; i--)
    {
        if (myParticles[i].Dead)
            myParticles.RemoveAt(i);
    }

    while (myParticles.Count > myBase.Particles)
    {
        myParticles.RemoveAt(myParticles.Count-1);      
    }
    #endregion


    //TODO: Do we really want 5000?  Can the number change to match our base particle count?
    ParticleSystem.Particle[] tempParticles = new ParticleSystem.Particle[5000];
    int count = emitter.GetComponent<ParticleSystem>().GetParticles(tempParticles);

    for (int i=0; i < count; i++)
    {           
        if (i >= myBase.Particles)
        {
            //This is more than we want, destroy it immediately and make sure that we don't get more than we really want.
            tempParticles[i].lifetime = -1;
        }
        else
        {
            myParticles[i].Update();
            if (tempParticles[i].lifetime < 5)
            {
                //Don't let it die!
                tempParticles[i].lifetime = 1024;
            }

            tempParticles[i].position = myParticles[i].GetPosition();

            //Debug.Log("hi");

            if (myParticles[i].ChangingColors)
            {
                tempParticles[i].color = myParticles[i].MyColor;
                if (tempParticles[i].color.a == 1)
                {
                    //This is the only place that ChangingColors is set to false so it should never stop fading in until I've faded all the way in.
                    myParticles[i].ChangingColors = false;
                }
            }


            if (!myParticles[i].SetUp)
            {
                //This is a brand new particle, so I need to tell it a few things.
                //We don't want it moving on it's own.
                tempParticles[i].velocity = Vector3.zero;
                tempParticles[i].axisOfRotation = new Vector3(Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f));
                myParticles[i].SetUp = true;
            }           
        }
    }
    emitter.GetComponent<ParticleSystem>().SetParticles(tempParticles, count);
}

Any ideas why this only works with a debug, and how to fix it?

\$\endgroup\$
1
\$\begingroup\$

So, in RotateParticleFinal I have a part that updates the alpha. That's only using floats. But when I check the actual particle to see if the alpha is at 1... well, it's a byte now. It was working better with a lot of debug calls because it slowed everything down, meaning I got a lot more alpha on my deltaTime based fade-in. Been wrestling with this for a while now and I didn't figure it out until right after I finally posted this >_<

\$\endgroup\$
  • 1
    \$\begingroup\$ Yep, when Debug.Log makes any difference, it's almost always because of timing. Unless you commit a cardinal sin like Debug.Log(++i); \$\endgroup\$ – piojo Jul 28 '15 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.