I figured the behavior for wanting projectiles and particle effects to delete themselves after a time could be handled by a single mechanism. I wanted to see if I really understood coroutines, so I tried this and added it to each of the impact effects and projectiles.
Projectiles got 20 seconds to find a target or perish. Impact effects would be configurable based on how long the effect is–I suspect I could make this look for a lifetime variable in the particle component, but I like how focused and clean this component is.
It works like a charm, and I can throw it onto anything without needing to edit other components.
public class SelfDestroyer : MonoBehaviour
{
[SerializeField] float lifetime = 10f;// Start is called before the first frame update void Start() { StartCoroutine(DestroySelf()); } IEnumerator DestroySelf() { yield return new WaitForSeconds(lifetime); Destroy(gameObject); }
}
It feels almost like “tagging” the item to have some common behavior. I think I’m starting to really understand how powerful components can be as function modules.