- Home /
TrailRenderer.clear removing character?
Hi, I'm looking to have a trail following the movement of my character as it's dragged around the level, and using a basic trailrenderer component achieves this fine. However I want the character to reset after it hits a wall, I've achieved this with the script below:
void OnCollisionEnter2D(Collision2D collision)
{
//If the Player hits a wall, scene resets and clears TrailRenderer particles
if (hasHitWall(collision))
{
this.transform.position = _startPosition;
tr.Clear();
}
}
I've also tried to slot in the "tr.Clear" ( with tr = GetComponent TrailRenderer(); in the Awake) to reset the trail particles to prevent them cluttering the screen with repeated resets of the player, however for some reason this also deletes the character entirely from the scene when he resets.
If I put the transform position and clear the other way around it works but obviously keeps the trail from the players position being reset which isn't ideal.
Can anybody help me understand how clearing the trail renderer removes the gameobject it's attached to? Have I screwed up royally here somewhere?
Answer by Curzd · Jun 07, 2021 at 06:23 AM
Solved after some actual consulting of the TrailRenderer API, I was not aware of autodestruct on the trail component which destroyed the component and the object it was attached to much to my alarm.