Search code examples
c#unity-game-enginecolorsparticle-system

Particle System color is pink when Start Color is changed


I am making brick breaker game and I want to display particle having same color as brick that gets hit by ball. Here is my code:

GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
ParticleSystem ps = smokePuff.GetComponent<ParticleSystem>();
ParticleSystem.MainModule psmain = ps.main;
psmain.startColor = gameObject.GetComponent<SpriteRenderer> ().color;

This is not working, the particle color is displayed Pink. How to fix it?

I'm using Unity 5.6.


Solution

  • This is a bug on some certain version of Unity. It should be fixed in Unity 2017.2. What happens is that when you change the ParticleSystem color, it loses its material reference.

    You can either update Unity to the latest version or manually attach that material reference or new material back to the ParticleSystem after setting the color.

    public GameObject smoke;
    
    void Start()
    {
        GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
        ParticleSystem ps = smokePuff.GetComponent<ParticleSystem>();
        ParticleSystem.MainModule psmain = ps.main;
        psmain.startColor = gameObject.GetComponent<SpriteRenderer>().color;
    
    
        //Assign that material to the particle renderer
        ps.GetComponent<Renderer>().material = createParticleMaterial();
    }
    
    Material createParticleMaterial()
    {
        //Create Particle Shader
        Shader particleShder = Shader.Find("Particles/Alpha Blended Premultiply");
    
        //Create new Particle Material
        Material particleMat = new Material(particleShder);
    
        Texture particleTexture = null;
    
        //Find the default "Default-Particle" Texture
        foreach (Texture pText in Resources.FindObjectsOfTypeAll<Texture>())
            if (pText.name == "Default-Particle")
                particleTexture = pText;
    
        //Add the particle "Default-Particle" Texture to the material
        particleMat.mainTexture = particleTexture;
    
        return particleMat;
    }
    

    EDIT:

    Two more things to know about creating Particle System and the pink particle issue:

    1. If you create your Particle System from the Component ---> Effects ---> Particle System menu, Unity will not attach material to the Particle System so it would be pink. You will have to use the code above to create new material or do it manually from the Editor. You will get pink ParticleSystem if you don't do this.

    Your problem is either this or the reference bug I described above .

    2. If you create your Particle System from the GameObject ---> Effects ---> Particle System menu, Unity will create new GameObject, attach a Particle System and a material to it. You should not have pink particle issue unless it is the bug I talked about particles losing material reference when color is modified.