- Home /
Change colour of particles with C#
Hello
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 ()
Code:
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[i].startColor = color;
}
particleSystem.SetParticles(particles,totalParticles);
}
}
What am i getting wrong here?
Answer by ComradeVanti · Mar 18, 2016 at 06:20 PM
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! :)
Your answer
Follow this Question
Related Questions
Flame Thrower Particle System problem with movement 0 Answers
Particle rotation simply doesn't work. 1 Answer
Particle System is not respecting sorting layer 0 Answers
Is it possible to simulate shuriken particule in World Space but follow parent tranform ? 1 Answer
Particle System Size over Lifetime overridden by min particle size 1 Answer