Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Destroy trail when finished rendering

Discussion in '2D' started by drpelz, Apr 18, 2018.

  1. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    I have a missile with a trail renderer attached to it.

    I want to destroy the game object (with the trail) after finishing the rendering process.

    Any help, please?

    The code below does not work:

    Code (CSharp):
    1.  
    2. private TrailRenderer tr;
    3.  
    4.  
    5. public void Start() {
    6.    tr = GetComponent<TrailRenderer>();
    7. }
    8.  
    9. public void Update() {
    10.  
    11.    if(tr) {
    12.  
    13.        if(tr.isVisible == false) {
    14.            Destroy(this.gameObject);
    15.        }
    16.    }
    17. }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    What does this mean?
     
  3. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    When you can see no trail any more.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Try setting the TralRenderer's autoDistruct property to true.
     
    Matdrox likes this.
  5. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    With this code I was able to finish the animation of the trail:
    Code (CSharp):
    1. private TrailRenderer tr;
    2.  
    3. void Start() {
    4.    tr = GetComponent<TrailRenderer>();
    5. }
    6.  
    7. public void MissileDestroyed()
    8. {
    9.    Destroy(gameObject, tr.time);
    10. }
     
    LiterallyJeff likes this.
  6. Kudorado

    Kudorado

    Joined:
    Nov 28, 2015
    Posts:
    10
    just add trail as child of missile.