Search code examples
androidunity-game-enginecolorsparticle-system

Unity - Particle system color not set properly in android build


I want to set the color of "Particle System" that I add to a game object in run time. both game object and particle system are created in run time too.

I have the following code that works well on PC when I run it... But the problem is that in "Android" builds, color do not set correctly and particles are always "pink".

It should be: enter image description here

But in Android it seen as: enter image description here

here is the code I use:

    GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);       
    sphere.transform.position = new Vector3(x, y, z);
    sphere.AddComponent<ParticleSystem>();
    var ps = sphere.GetComponent<ParticleSystem>();
    ps.startLifetime = 1;
    ps.startSpeed = 0.01f;
    ps.startSize = 0.03f;
    ps.maxParticles = 10000;
    ParticleSystemExtension.SetEmissionRate(ps, 10000);
    var sh = ps.shape;
    sh.shapeType = ParticleSystemShapeType.Circle;
    sh.radius = 0.69f;

    var cbl = ps.colorOverLifetime;
    cbl.enabled = true;
    Color mater = Color.green;
    cbl.color = new ParticleSystem.MinMaxGradient(mater);

Solution

  • Oh God! I change my solution and it work fine!

    At first I make an empty GameObject with ParticleSystem attached, then create a prefab of that. after that, I add this prefab to my scene and set the necessary properties to it and then deactivate that.

    In my script, add a public GameObject and assigned it to my gameObject (called Ring) that was created before.

    Then instatiate it by the following code:

    GameObject ps = Instantiate(Ring);
    

    then make ps the child of spheres that I was creating in code:

     Ring.GetComponent<ParticleSystem>().startColor = Color.green;
     GameObject ps = Instantiate(Ring);
     ps.transform.parent = sphere.transform;
     ps.transform.localPosition = Vector3.zero;
     ps.SetActive(true);
    

    And that's it! It works nice!