I’m trying to setup the particles so that they are in a circle and the y values are randomly generated. For some reason, it doesn’t seem to enter the for loop.
I’m not getting any errors, so I’m at a standstill at why it’s not working.
function Start()
{
particleSetup();
}
function particleSetup()
{
var radius = 5;
var numParticles = 10;
var intervals = 2*Mathf.PI/numParticles;
particleEmitter.minEmission = numParticles;
particleEmitter.maxEmission = numParticles;
var particles;
particles = particleEmitter.particles;
for (var i=0; i<particles.Length; i++)
{
Debug.Log("ME");
var xPosition = Mathf.Sin(intervals*i)*radius;
var yPosition = Random.value;
var zPosition = Mathf.Cos(intervals*i)*radius;
particles[i].position = Vector3(xPosition, yPosition, zPosition);
}
particleEmitter.particles = particles;
}
Debug.Log(“Me”) never gets called if i have it inside the for loop. However, if I change particlesSetup() to LateUpdate() so that it runs every frame, it will show up in the log. Any help is appreciated.
Tried length with a lowercase “l”? It’s the only thing that jumps out at me.
A lowercase “l” would not be correct; the way it is now is fine. The reason the for loop is never entered is because the length is zero, so “i < 0” is never true. You need to emit some particles before particles.Length would be more than zero. Also don’t do stuff like:
var particles;
particles = particleEmitter.particles;
That uses dynamic typing, which is slow. Just make that
var particles = particleEmitter.particles;
Also it would be better to make “radius” and “numParticles” public variables so you can change them easily in the Inspector.
–Eric
Length has to be capital because its a Unity variable. I’m not having compile errors. It’s just the code inside the for loop doesn’t seem to want to run.
Eric was right, it was because Length was at zero and that was because in Unity, the minEmission and maxEmission were set to zero. However, I’m supposed to be setting these two values in the Start(), but they never get set with the value I give it.
Sorry about the mixup. I guess I’m too used to the lowercased variables at this point.
Well, it doesn’t matter what the emission is, as it was zero at the start of the frame that you attempt to enter the for loop in. Basically, nothing will emit until the next frame.
So, yield for one frame before the for loop and you should be fine.
If you want to create particles in a circle you should emit them in a loop:
http://unity3d.com/support/documentation/ScriptReference/ParticleEmitter.Emit.html
As Eric mentioned, you don’t have any particles to begin with here…
Thanks for answering my questions.
I’m trying to create a mesh for each particle and parent them to the particle, so that when the particles move, the mesh’s will follow. Currently, I’m creating a Game Object for each particle’s location with its own Mesh Filter and Mesh Renderer. Is there a way to parent the Game Objects to the particles?