Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Particle System pink when attached to a GameObject

I am working on unity particle system.

I created a new project and an empty object then added the Particle System to it. For some reason it not working properly you can see the image attached to see whats happening...

enter image description here

like image 430
I AM A Hacker Avatar asked May 08 '18 14:05

I AM A Hacker


1 Answers

The problem is a missing material due to how you created the particle.

There are two ways to create Particle System:

1.Create empty GameObject, select it then go to Component --> Effects and add the Particle System component to that empty GameObject. This is how you created your current Particle System.

If you create your Particle System with method #1, Unity will not attach material to the Particle System therefore making it to be pink. You will have to create a new Material, change the shader to "Particles/Alpha Blended Premultiply" and use the "Default-Particle" as the texture to make the particle look like the default material.

You can also just use the "Default-Material" for the Particle System but you can't modify it.

enter image description here

2.Create particle by going to GameObject ---> Effects ---> Particle System.

If you create your Particle System with method #2, Unity will create new GameObject, attach a Particle System and also a material to it.

Always create your material by going to GameObject ---> Effects ---> Particle System. It will save you some time.

The simple solution is to delete your current particle GameObject, create new one by going to GameObject ---> Effects ---> Particle System instead of the method described in #1.

If you need to create Particle System from code then do what I said in method #1 but via script. Here's how to do that:

void Start()
{
    createParticleSys();
}

void createParticleSys()
{
    //Create GameObject to hold the Particle System
    GameObject psObj = new GameObject("Particle System");
    //Add Particle System to it
    ParticleSystem ps = psObj.AddComponent<ParticleSystem>();

    //Assign 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;
}
like image 95
Programmer Avatar answered Sep 27 '22 19:09

Programmer