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. Dismiss Notice

Destroying clone particles

Discussion in 'Scripting' started by Zyie, Jan 17, 2015.

  1. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
    I'm trying to get my game to destroy the particle clones however when I use the Destroy() function on the gameObject i get an MissingReferenceException error

    So how do I get rid of these particles once I instantiate a new one?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,502
    You should probably post the relevant snippet of code. What is that .gameObject you are destroying? The thing making the particle? The particle system game object itself? Something else?

    Basically you want to Instantiate() the particle system, which ought to go on a fresh GameObject, and then destroy it when you're done.
     
  3. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
    This is the code I have that is currently spawning the particles when the player is doing a double jump. What I don't know how to do is to destroy the particle once it has spawned and then be able to spawn another one.

    Code (CSharp):
    1.  
    2.  
    3. public GameObject particle;
    4.  
    5.        void Update()
    6.     {
    7.        
    8.         if ((grounded || !doubleJump) && Input.GetButtonDown("Jump"))
    9.         {
    10.             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
    11.             rigidbody2D.AddForce(new Vector2(0f,jumpForce));
    12.             audio.Play();
    13.  
    14.             if (!grounded && Input.GetButtonDown("Jump"))
    15.                     Instantiate(particle,transform.position, transform.rotation);
    16.  
    17.            
    18.  
    19.             if (!doubleJump && !grounded)
    20.                 doubleJump = true;
    21.         }
    22.     }
    23. }
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You need to keep a reference of the instantiated object and destroy it.

    Code (CSharp):
    1. ParticleSystem go = Instantiate(particle,transform.position, transform.rotation) as ParticleSystem;
    2.  
    3. Destroy(go.gameObject, go.duration);
    Something like that, I'm playing can't help you more for now
     
  5. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
    Adding this gives me this error

    NullReferenceException: Object reference not set to an instance of an object
    Movement.Update () (at Assets/Scripts/Movement.cs:49)