Search Unity

Changing Particles' Color through Code

Discussion in 'Scripting' started by yangmeng, Jan 19, 2009.

  1. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    I tried using this code to change the color of particles in my game but it doesn't seem to do anything:
    Code (csharp):
    1. myParticles.renderer.material.SetColor( "_TintColor", Color.green);
    I could just make another particle emitter with the color I want and switch between them, but I would prefer to do this through code. Any suggestions?
     
  2. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    tried setting the particle animators colors yet?
     
  3. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Yep, but I want to change the colors at runtime depending on what's going on in the game - so for example, start with particles that start out yellow and red for rocket thrusters and change it to gray and black for smoke if the engines get hit kind of thing.
     
  4. hockeybag

    hockeybag

    Joined:
    Jan 25, 2007
    Posts:
    29
    Why not make prefabs of each particle effect you need and instantiate the right one at the right time?
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I don't think the particle animator colors are scriptable, unfortunately.

    --Eric
     
  6. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    That's basically how I am doing it, but I thought scripting would be a better choice. Looks like I'll keep things the way they are, since scripting is not an option.
     
  7. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Code (csharp):
    1. myParticles.renderer.material.SetColor( "_TintColor", Color.green);
    This works for me providing I use a shader that has the _TintColor attribute. You might need to turn off yourParticleAnimator.doesAnimateColor to stop it from interfering with your colours.

    Also, I've just noticed that ParticleAnimator.colorAnimation was added in 2.1 as a means of setting the colours that the ParticleAnimator will cycle through.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oh, cool...how did I miss that? Good to know.

    --Eric
     
  9. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    Not working for me (version 2.5 indie)

    From the doc linked above:
    var modifiedColors : Color[] = particleAnimator.colorAnimation;

    I get 'Unknown Identifier: particleAnimator'
    Using GetComponent doesn't help either.
    Help!