Why does my code only work when there is a debug statement? (Unity)

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++)
{
}
}

void Update ()
{
#region Deal with changing particle count
while (myParticles.Count < myBase.Particles)
{
}

for (int i=myParticles.Count-1; i >= 0; i--)
{
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.
}
else
{
myParticles[i].Update();
{
//Don't let it die!
}

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?

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