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

Particle System Start color not changing

Discussion in 'Scripting' started by BenjaFriend22, Mar 14, 2017.

  1. BenjaFriend22

    BenjaFriend22

    Joined:
    Sep 29, 2016
    Posts:
    13
    So I am just trying to simply change the start color of a particle system via script, and it's not working.

    Code (CSharp):
    1.      private ParticleSystem trailPartical;   // The  particle system
    2.  
    3.      public Color StartColor
    4.      {
    5.           var main = trailPartical.main;
    6.           main.startColor = value;
    7.      }
    This is not working at all, and I have also tried the depreciated version:

    Code (CSharp):
    1. trailParticle.startColor = value;

    Unity 5.5.1f
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Well, I know that you have compile errors because that wouldn't compile. In the future, post them. If you didn't know you had compile errors, you need to check Unity's Console for them.

    Your main logic (e.g. lines 5 and 6) is fine (though incomplete), so the reason it isn't working is because the "function" they're in is... not a thing. It looks like you've taken a little bit of syntax from a bunch of different things in C# and mashed them together. And my guess is that you're not calling that piece of code anywhere, either. And you will need to assign trailParticle somehow; based on what I see there, it's always going to point to nothing.

    If for example you want to change the color when the script first starts, you need to put it in the Start function.

    Code (csharp):
    1. public class YourScriptNameHere : MonoBehaviour {
    2. private ParticleSystem trailParticle;
    3. public Color myColor = Color.blue;
    4.  
    5. void Start() {
    6. trailParticle = GetComponent<ParticleSystem>();
    7. trailParticle.main.startColor = myColor;
    8. }
    9. }
     
  3. GLBL13

    GLBL13

    Joined:
    Oct 22, 2016
    Posts:
    2
    If the value is a public variable and set using editor be sure its alpha is not 0. Editors default color picker starts with 0 alpha value and you may forget to change it.
     
  4. Klaus-Eiperle

    Klaus-Eiperle

    Joined:
    Mar 10, 2012
    Posts:
    41
    I'm sure you have found out the way it works. When you use StarManta's example in Unity3D 5.6, you get only an error, because you should take the MainModule into an instance. This is a simple particle pool which reads out the start color and writes it back with modified alpha when the particle is used. The script is attached to the particle prefab which was instanced into an particle pool game object (acts as folder) at the beginning of the game.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObjectPoolParticle : MonoBehaviour {
    5.  
    6.     float live=1.0f;  // this is the live duration of an particle until it will be deactivated automatically
    7.     float timeToDie=0.0f;
    8.     ParticleSystem _particleSystem;
    9.     ParticleSystem.MainModule _main;
    10.  
    11.     void Awake() {   // here we get the references
    12.         _particleSystem = this.gameObject.GetComponent<ParticleSystem>();
    13.         _main = _particleSystem.main;
    14.     }
    15.  
    16.     void Update () {  // here we count down the live duration and deactivate it when it's live is over
    17.         if (timeToDie < Time.time) {
    18.             _particleSystem.Stop(false, ParticleSystemStopBehavior.StopEmittingAndClear);
    19.             this.gameObject.SetActive(false);
    20.         }
    21.     }
    22.  
    23.     public void Activate(Vector3 _pos, float _alpha) {
    24.         // this is the function which is called from outside to activate the particle. It takes the position and a alpha
    25.         timeToDie = Time.time+live;
    26.         transform.position = _pos;
    27.  
    28.         Color _color = _main.startColor.color;
    29.         _main.startColor = new Color (_color.r, _color.g, _color.b, _alpha);
    30.         _particleSystem.Play();
    31.     }
    32. }