Trail Renderer is not being destroyed

The scripts are 100% accurate and AutoDestruct is checked in the TrailRenderer. Using Unity 2022.3.2f1 on a Mac. Any ideas?

Are you saying that the renderered trail is never fading away, or that after it has the gameobject is still hanging around in the scene?

I am on the Shooting Bullet Visual section and have completed that, But, in play mode, my Trail Renderer does not get destroyed. All the relevant scripts are 100% correct. Quadruple checked. Auto Destruct is checked in the Trail Renderer. Everything else up to that point works flawlessly. I am using Unity 2022.3.2f1 on a Mac. Any ideas, I have no clue at this point.

Pretty sure it is the Trail Renderer.

So the trail is never fading away?

The trail renderer should be getting a double dose of destroyed, as it’s attached to the bullet (which is destroyed when it reaches it’s destination) and should be set to destroy on finish.

Let’s take a look at your BulletProjectile.cs, and the inspector for your Trail Renderer.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletProjectile : MonoBehaviour
{

	[SerializeField] private TrailRenderer trailRenderer;
	[SerializeField] private Transform bulletHitVfxPrefab;

	private Vector3 targetPosition;

	public void Setup(Vector3 targetPosition)
	{
		this.targetPosition = targetPosition;
	}

	private void Update()
	{
		Vector3 moveDir = (targetPosition - transform.position).normalized;

		float distanceBeforeMoving = Vector3.Distance(transform.position, targetPosition);

		float moveSpeed = 200f;
		transform.position += moveDir * moveSpeed * Time.deltaTime;

		float distanceAfterMoving = Vector3.Distance(transform.position, targetPosition);

		if (distanceBeforeMoving < distanceAfterMoving)
		{
			transform.position = targetPosition;

			trailRenderer.transform.parent = null;

			Destroy(gameObject);

			Instantiate(bulletHitVfxPrefab, targetPosition, Quaternion.identity);
		}
	}

}

I think it may have something to do with Unity itself. Something I missed, or a compatibility issue with my Unity version or Mac.

It’s quite possible it’s yet another 2022.3 glitch. While I’m on PC, the list of issues with Unity 2022.3x reads like the warnings after a medication commercial. (Usually LTS means “Long Term Support”… I’ve found in 2022’s case it means “Less Than Satisfactory”)…

Are you getting any errors in the inspector when the bullet fires?

Let’s try adding a brute force approach

No errors. Everything works flawlessly up to this point.

The added Destroy command should force the issue.

Nope.

Add this script to the Trail Renderer’s GameObject:

using UnityEngine;


    public class DestroyAfter : MonoBehaviour
    {
        [SerializeField] private float destroyTime = .2f;
        private void Start()
        {
            Destroy(gameObject, destroyTime);
        }
    }

That worked. Thanks. Still going to drive me batty until I nail down what caused the issue. But yes, likely a Unity version issue.

It must be. Sometimes we just have to push through bugs like this with something less than finesse:

1 Like

Ha, very true. Thanks for that fast reply.

Privacy & Terms