TrailRenderer with object pooling causes unfortunate effects

I’m using a trail renderer with my projectiles, of which I dynamically pool. After the projectile has exceeded it’s range it is immediately set as inactive and waits to be triggered again. However when this happens there’s a split second where the trail renders from the position the projectile was laying dormant. I’ve tried several things, like disabling the trail renderer and re-enabling in the OnEnable() call. I’ve tried setting the TrailRenderer.time to 0f until the OnEnable call. None of that is doing the trick.

Let me know if I need to explain or detail anything else. It’s a super obnoxious effect and I hope I can get it resolved soon.

Thanks in advance!

1 Like

I know this post is old, But just in case somebody stumbles across it like me looking for answers.

onEnable of this pooled object call TrailRenderer.Clear();

This fixed it for me.

27 Likes

This helped very much. Thank you.

3 Likes

Thank you very much. It saves my life

1 Like

Hell yeah, worked for me, now bullet trails don’t look weird when pooling :slight_smile:
Here it is if someone wants a script to just slap onto the gameobject:

using UnityEngine;
public class TrailPlugin : MonoBehaviour
{
    TrailRenderer trailRenderer;
    private void Awake()
    {
        trailRenderer = GetComponent<TrailRenderer>();
    }
    private void OnEnable()
    {
        trailRenderer.Clear();
    }
}
3 Likes

Years later, still fixing many peoples issues! Many thanks for posting this even if the original post is years old.

Thank you very much…

Awesome thank you! (:

Thanks for the script! Worked perfectly!

It didn’t work with OnEnable for me (probably because of how I set up my code) so I did it just when setting the bullet object to inactive (when it hits a target/ covers max range) and then it worked for me perfectly! Just in case this helps anyone. Thanks a lot for the response!

 if (trailRenderer != null) trailRenderer.Clear();

gameObject.SetActive(false);

Just Untick auto destruct in trail renderer

Fully worked thanks a lot!

Stop necro-posting. There’s a Like button on every post so you can express approval without dredging up old threads on people’s feeds as new.

I know it’s a late answer. But for new people seeking for a solution to this unfortunate effect.

Instead of clearing trail effect on enable (When object being pooled from pool). Try clearing it on object Release() (When you return the pooled object to the pool).

void Awake() {
   _trailRenderer = gameObject.GetComponent<TrailRenderer>();
}

void Release() {
    /* Clear trail effect 
    *  (Can add all effects to be reset or re-configured here) 
    */
   _trailRenderer?.Clear();
   // Return object to pool in order to disable it and be ready for reuse
   _pool.ReturnToPool(this);
}

I hope this will help!