- Home /
Particle System won't play in method.
So i'm trying to make it so that when two specific objects collide they destroy each other and play an explosion, I've already sorted out the collision and destroying but for some reason the particle system just won't play, if I set the same "particlesystem.play();" to a button input it works just fine but not in a method, please help!
ps: the script is on PlayerYellow.
public class PlayerCollision : MonoBehaviour
{
public GameObject PlayerBlue;
public GameObject PlayerYellow;
public ParticleSystem YellowExplosion;
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "PlayerBlue")
{
Explosion();
DestroyPlayers();
}
}
void DestroyPlayers()
{
Debug.Log("Destroy");
Destroy(PlayerBlue);
Destroy(PlayerYellow);
}
void Explosion()
{
Debug.Log("Explosion");
YellowExplosion.Play();
}
}
Is the particle system attached to the object being destroyed? If so, then, obviously, it can't play since you're destroying it.
If the particle system is attached/ a child of PlayerYellow or PlayerBlue, then it's obviously going to get destroyed alongside PlayerYellow and PlayerBlue. The solution would be to Instantiate a YellowExplosion prefab at PlayerYellow's position, then playing it from there.
void Explosion()
{
Debug.Log("Explosion");
ParticleSystem yExplosion = Instantiate(YellowExplosion,PlayerYellow.transform.position,Quaternion.identity);
yExplosion.Play();
}