- Home /
How to properly Play() a particle system onCollisionEnter if particle system is a child of enemy shot?
Hi I have a game object enemy, when I shoot it, the bullet collision calls a coroutine to disable the enemy back into its object pool.. That was working fine.I wanted to add an explosion on the enemy.
The particle system is a child of the enemy. I use GetComponentInChildren in start to get the particle system. In play I can see it set the particle system in the editor yet I am still getting a null ref exception pointing to my line of code explosion.Play();
But I don't understand why, any ideas?
public class Enemy : MonoBehaviour {
public ParticleSystem explosion;
public void Start()
{
explosion = GetComponentInChildren<ParticleSystem>();
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Projectile"))
{
UpdateEnemyCountdown();
StartCoroutine(EnemyShot());
}
}
private void UpdateEnemyCountdown()
{
Score.enemyCountdown--;
}
public IEnumerator EnemyShot()
{
explosion.Play();
yield return new WaitForSeconds(3);
ObjectPooler.Instance.DeactiveEnemy(gameObject);
yield return null;
}
}
Your answer

Follow this Question
Related Questions
GetCompontentInChildren erratic behaviour 0 Answers
Setting particle position every frame... 1 Answer
Multiple instances of an Particle System prefab have the exact same animation. 1 Answer
How to make a mask that shows an invisible image from behind it 0 Answers
Particle System won't work right. 3 Answers