- Home /
C# Particle emissionRate does nothing
The basic premise is that I want the player's character to sparkle as they gather pickup items. I could do it with an animation, but I'm really hoping to use particles.
But my code. It does nothing.
At present, the player has four particle emitters attached as children. Four because each is a different color. Each is set to the specs I want to get the desired effect, but I've set their emission rate at 0. I had hoped to use scripts to change this as pickup items are gathered.
This is the relevant code applied to each pickup item:
public class SparklePickup : MonoBehaviour {
public float sparklesToAdd;
void OnTriggerEnter2D (Collider2D other){
if (other.GetComponent<PlayerController> () == null)
return;
PlayerSparkle.AddSparkles (sparklesToAdd);
Destroy (gameObject);
}
}
And this is the relevant code applied to each of the particle systems:
public class PlayerSparkle : MonoBehaviour {
public static float playerSparkles;
void Start () {
playerSparkles = GetComponent<ParticleSystem> ().emissionRate;
}
void Update () {
}
public static void AddSparkles(float sparklesToAdd){
playerSparkles += sparklesToAdd;
}
public static void RemoveSparkles(){
}
}
Everything seems to fire just fine, I've thrown in Debug.Log() items here and there to get some feedback. Except that my particle emission rate never increases, no matter how many pickup items I gather. Wat do?
Answer by zombience · Apr 07, 2015 at 05:42 PM
in your setup you'd need to make sure that your particle system is set to play on awake and has emission enabled (enableEmission = true) and looping is set to true.
starting up a scene with default particle emitter and attaching this script to it does exactly what you want:
using UnityEngine;
using System.Collections;
public class ParticleEmission : MonoBehaviour
{
protected ParticleSystem ps;
public float emissionRate;
public bool emit;
void Start ()
{
ps = particleSystem;
}
void Update ()
{
ps.emissionRate = emissionRate;
if(emit)
{
ps.Emit((int)emissionRate);
emit = false;
}
}
}