Change colour of particles with C#


I have a Particle System with particles that have an infinite lifetime. But i want to make these particles change colour, or rather, change their alpha over time using a random value.

I appled this code, but get this error:

NullReferenceException: Object reference not set to an instance of an object
Flicker.Update ()


using UnityEngine;
using System.Collections;

public class Flicker : MonoBehaviour {

	ParticleSystem particleSystem;
	ParticleSystem.Particle[] particles;

	private int totalParticles;
	private Color32 color;
	void Start(){
		particleSystem 		= GetComponent<ParticleSystem>();
		totalParticles 		= particleSystem.particleCount;

	void Update(){

	for(int i = 0; i < totalParticles; i++){
		color = new Color(1,1,1,Random.Range(0.8f,1));
		particles*.startColor = color;*
  • }*

  •  particleSystem.SetParticles(particles,totalParticles);*
  • }*
    What am i getting wrong here?

You need to set your particles… They are an empty variable in your script.

Use this as your first line in Update

totalParticles = particleSystem.GetParticles(particles);

Good luck! :slight_smile: