BenjaFriend
BenjaFriend

Reputation: 664

How to change the Start color of the Particle System

So I am just trying to simply change the start color of a particle system via script, and it's not working.

 private ParticleSystem trailPartical;   // The  particle system

 public Color StartColor
 {
      var main = trailPartical.main;
      main.startColor = value;
 }

This is not working at all, and I have also tried the depreciated version:

 trailParticle.startColor = value;

Upvotes: 1

Views: 16615

Answers (3)

Dave
Dave

Reputation: 2842

In case anybody wondering how to set the gradient:

ParticleSystem.MainModule psMain = GetComponent<ParticleSystem>().main;
psMain.startColor = new ParticleSystem.MinMaxGradient(Color.white, Color.red);

Upvotes: 1

Programmer
Programmer

Reputation: 125275

I think I know what you are trying to do. You want to simplify setting the color with just one function or property.

You will get this error with your current code:

A get or set accessor expected.

That's because you did not implement the set accesstor.

That property should be like this:

private ParticleSystem trailPartical; 

public Color StartColor
{
    set
    {
        var main = trailPartical.main;
        main.startColor = value;
    }
}

then...

void Start()
{
    trailPartical = GetComponent<ParticleSystem>();
    StartColor = Color.red;
}

This should work.

Upvotes: 2

Galandil
Galandil

Reputation: 4249

You're trying to use StartColor as a method, judging by the code inside the {}, even though you declared it as a variable.

Apart from this mistake, due to some changes in the ParticleSystem, you need to access the main module of the component:

ParticleSystem.MainModule main = GetComponent<ParticleSystem>().main;
main.startColor = Color.blue; // <- or whatever color you want to assign

inside a script attached to the game which also has the particle system component.

Upvotes: 10

Related Questions