How to make trail renderer lasts after game object destroyed?

I’m trying to have trail renderer following my bullets. However, I want the trail renderer to still exist until the duration ends even after the bullet is destroyed.

1 Answer

1

If you want to have the trail survive after the bullet is destroyed, first make sure that the trail renderer is attached to a child object of the main bullet. Then, in your bullet object, make sure you have a reference to the trail renderer (set it up in the inspector), then do something like this:

void OnDestroy()
{
    trail.transform.parent = null;
    trail.autodestruct = true;
}

This will make the trail detach, and then destroy iteslf when the trail expires.

Adding to @syclamoth, you should also set the reference to null to prevent leaks: void OnDestroy() { trail.transform.parent = null; trail.autodestruct = true; trail = null; } As a general rule, I use the OnDestroy() method to release all references. Not doing so may result in memory waste, especially on iOS.

The garbage collecter should handle this for you, though. Have you got any evidence that this decreases memory leakage?