Whenever a coin is collected in my game, it generates a particle system clone in the hierarchy when the effect plays. To avoid clutter, I want those clones to be destroyed after a set time. However, I can’t seem to get this to work. My coin remains when it is supposed to disappear, and the particle system doesn’t delete itself from the hierarchy.
public Transform coinEffect;
float delayTime = 3;
void OnTriggerEnter (Collider info) {
if (info.tag == "Player") {
GameObject effect = GameObject.Instantiate(coinEffect, transform.position, transform.rotation) as GameObject;
Destroy (effect.gameObject, delayTime);
Destroy (gameObject);
}
}
The error I get when this code runs is "NullReferenceException: Object reference not set to an instance of an object
coinPickup.OnTriggerEnter (UnityEngine.Collider info) (at Assets/scripts/coinPickup.cs:15)
"
I’m still pretty new to Unity so I’m not sure what this error is telling me. Line 15 where the error says there’s a problem is the “Destroy (effect.gameObject, delayTime);” line. Any help is appreciated.