Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to change the colour of more than one particle simultaneously?

Discussion in 'Scripting' started by Hotpots, Apr 30, 2016.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    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)
    Code (CSharp):
    1.     public ParticleSystem riftParticle;
    2.  
    3.     void Update(){
    4.         FadeParticleColor (Color.black, 5);
    5.     }
    6.  
    7.     public void FadeParticleColor(Color targetColor, int seconds)
    8.     {
    9.         riftParticle.startColor = Color.Lerp (riftParticle.startColor, targetColor, Time.time / seconds);
    10.     }
    Any help would be kindly appreciated as having to make 100+ public references would be INSANE :p
     
  2. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Still unsure of a solution :/
     
  3. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Is it even possible to do something like this? Is my approach wrong?
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    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.
     
  5. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    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:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ParticleSystemItem : MonoBehaviour {
    4.  
    5.     void OnEnable(){
    6.         ParticleSystemManager.Add(this);
    7.     }
    8.  
    9.     void OnDisable(){
    10.         ParticleSystemManager.Remove(this);
    11.     }
    12.  
    13. }
    You then would create the manager that looks something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class ParticleSystemManager {
    5.  
    6.     protected static List<GameObject> items = new List<GameObject>();
    7.  
    8.     public static void Add(ParticleSystemItem item){
    9.         if (!items.Contains(item.gameObject)) {
    10.             items.Add(item.gameObject);
    11.         }
    12.     }
    13.  
    14.     public static void Remove(ParticleSystemItem item){
    15.         if (items.Contains(item.gameObject)) {
    16.             items.Remove(item.gameObject);
    17.         }
    18.     }
    19.  
    20.     public static GameObject[] GetItems(){
    21.         return items.ToArray();
    22.     }
    23.  
    24. }
    In your script you would then call this somewhere:

    Code (CSharp):
    1. GameObject[] items = ParticleSystemManager.GetItems();