At the moment what I am attempting to do is, based on user interaction I want the colour of the particle system to fade to a different colour. I am referencing the particle system through a public variable in the inspector and then dragging it. However if I want to drag the parent of ALL my particle systems it won’t let me which is very unusual, as I’m successfully achieving this whilst doing other things like scale, position, material colour etc…
My code here (even though it’s working as intended)
public ParticleSystem riftParticle;
void Update(){
FadeParticleColor (Color.black, 5);
}
public void FadeParticleColor(Color targetColor, int seconds)
{
riftParticle.startColor = Color.Lerp (riftParticle.startColor, targetColor, Time.time / seconds);
}
Any help would be kindly appreciated as having to make 100+ public references would be INSANE
Still unsure of a solution
Is it even possible to do something like this? Is my approach wrong?
is colour over life not doing this for you? or you want to do it across all particles regardless of their lifetimes? If that is the case you might be able to do this via the shared material, or you could get a array of particles with the ParticleSystems GetParticles method and than loop over the resulting array and play with their start colors. If you did take that approach you will likly have to turn off all modules that change the particle colour since they would overwrite the colour on you.
If I am understanding you correctly, you have multiple particle systems. If that is correct, I would make a public static lists, and add/remove items to/from that list. You would then just loop through that list.
For each particle system you would add this:
using UnityEngine;
public class ParticleSystemItem : MonoBehaviour {
void OnEnable(){
ParticleSystemManager.Add(this);
}
void OnDisable(){
ParticleSystemManager.Remove(this);
}
}
You then would create the manager that looks something like this:
using UnityEngine;
using System.Collections.Generic;
public class ParticleSystemManager {
protected static List<GameObject> items = new List<GameObject>();
public static void Add(ParticleSystemItem item){
if (!items.Contains(item.gameObject)) {
items.Add(item.gameObject);
}
}
public static void Remove(ParticleSystemItem item){
if (items.Contains(item.gameObject)) {
items.Remove(item.gameObject);
}
}
public static GameObject[] GetItems(){
return items.ToArray();
}
}
In your script you would then call this somewhere:
GameObject[] items = ParticleSystemManager.GetItems();