Lag When Instantiating Particle System Prefab On Collision
I am currently working on a game where a particle system prefab's color is changed to be the color of an object that the player collides with, and is then instantiated. This is causing considerable lag after the collision occurs, and my cursor in the editor when this lag occurs goes to the loading cursor. Here is the code in question:
void OnCollisionEnter2D(Collision2D coll)
{
Color targetColor = coll.gameObject.GetComponent<SpriteRenderer>().color;
ParticleSystem.MainModule particles = explosionParticles.GetComponent<ParticleSystem>().main;
particles.startColor = targetColor;
GameObject explosion = Instantiate(explosionParticles, transform.position, Quaternion.identity);
explosion.GetComponent<ParticleSystem>().Play();
Destroy(gameObject);
Destroy(coll.gameObject);
GameController.IncreaseScoreUI();
}
where explosionParticles
is the prefab that contains the particle system I wish to play on collision. I'm not sure how to do this properly to avoid this lag, any help is appreciated!
Your answer
Follow this Question
Related Questions
ParticleSystem Prefab Preview in EditorWindow 0 Answers
Act like a Trigger only with specific object 0 Answers
Particle Field not emitting at all 1 Answer
Texture Sheet Animation broken in 2020.3.7f1? 0 Answers
Particle system BUG??? 0 Answers