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 does not set correctly and particles are always "pink".

It should be:


But in Android it seen as:


Here is the code I use:

GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);       
sphere.transform.position = new Vector3(x, y, z);
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);

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;

And that's it! It works nice!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.