Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Problems with particles and code

Discussion in 'Scripting' started by Timelier, Aug 29, 2008.

  1. Timelier

    Timelier

    Joined:
    Aug 14, 2008
    Posts:
    17
    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.


    Code (csharp):
    1.  
    2. function Start()
    3. {
    4.     particleSetup();
    5. }
    6.  
    7. function particleSetup()
    8. {    
    9.     var radius = 5;
    10.     var numParticles = 10;  
    11.     var intervals = 2*Mathf.PI/numParticles;
    12.    
    13.     particleEmitter.minEmission = numParticles;
    14.     particleEmitter.maxEmission = numParticles;
    15.    
    16.     var particles;
    17.     particles = particleEmitter.particles;
    18.    
    19.     for (var i=0; i<particles.Length; i++)
    20.     {  
    21.         Debug.Log("ME");
    22.         var xPosition = Mathf.Sin(intervals*i)*radius;
    23.         var yPosition = Random.value;
    24.         var zPosition = Mathf.Cos(intervals*i)*radius;
    25.         particles[i].position = Vector3(xPosition, yPosition, zPosition);
    26.     }
    27.    
    28.     particleEmitter.particles = particles;  
    29. }
    30.  
    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.
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Tried length with a lowercase "l"? It's the only thing that jumps out at me.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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:

    Code (csharp):
    1.     var particles;
    2.     particles = particleEmitter.particles;
    That uses dynamic typing, which is slow. Just make that

    Code (csharp):
    1. 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
     
  4. Timelier

    Timelier

    Joined:
    Aug 14, 2008
    Posts:
    17
    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.
     
  5. Timelier

    Timelier

    Joined:
    Aug 14, 2008
    Posts:
    17
    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.
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    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.
     
  7. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  8. Timelier

    Timelier

    Joined:
    Aug 14, 2008
    Posts:
    17
    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?