Destroy explosion

hi there!
i’m kinda new to unity and the community, so hello everyone who minds to answer :slight_smile:

i’m trying to write a code for a missile hitting a srf.
the missile is spawned by a launcher as a clone.
when the missile hits something it generate an explosion as an instanced particle sys.

the point is, whil i’m able to destroy the missile with destroy(gameobject), i cannot do the same with the explosion, because (my understanding is) it actually destroy the original item instead of the cloned one.

i get this erroe, which is quite clear to me.

MissingReferenceException: The variable explosion of mssl_explode doesn’t exist anymore.
You probably need to reassign the explosion variable of the ‘mssl_explode’ script in the inspector.

any idea for a workaround?

here is the script applied to the missile.

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

public class mssl_explode : MonoBehaviour
{
public ParticleSystem explosion;



    void OnCollisionEnter (Collision other)
    {
        Debug.Log ("hit");
		Instantiate(explosion, transform.position, Quaternion.identity);
		Destroy (gameObject);
		Destroy(explosion, explosion.main.duration);

	}
}

I see a couple of problems:
One problem is that you are trying to destroy the prefab itself, not the instantiated explosion.
When you instantiate, write the instantiated object in a variable and then give that to the Destroy function.

Second,when you destroy the gameobject you destroy also the Monobehavior, I think, and you loose the content of explosion. I would try to Destroy the main gameObject the last, when you did everything else.
I would try something like this:

instanceExplosion = Instantiate(explosion, …
Destroy(instanceExplosion, explosion.main.duration)
Destroy(gameobject)

Anyway, depending if you have to generate missiles often, I would suggest to deactivate the missile and send it back to a pool in the launcher. This would be more performant than Instantiating missiles and deleting them the whole time. I have a similar use case in my naval simulation project with cannon-balls. The cannon has a pool of projectiles and when they hit the target, the simulation deactivates the cannonball(s) & particle effects and send them back to the pool for the next use

By the way what do you mean with srf? I’m curious

thanks, this was a great hint!
i didn’t think about creating a variable with the instance in it (me = noob).

also your solution with the cannonballs seems interesting, but i have no idea how to code it :stuck_out_tongue:

srf, abbreviation for surface. that’s something i use as a autodesk alias modeller.

i’m just making the missiles explode when they hit the scenery.
as soon as i understand how to discriminate weather the objects that has been hit is a a target or a scenery i will try to implement them :slight_smile:

update:
tryed to put the instance in a variable, but it doesn’t get destroyed.

    void OnCollisionEnter (Collision other)
    {
        Debug.Log ("hit");
		clonexpl = (Instantiate(explosion, transform.position, Quaternion.identity));
				Destroy(clonexpl, explosion.main.duration);
				Destroy (gameObject);


	}

That’s because you are destroying only the particle system and not the entire gameobject. You can use cloneexpl.gameObject to get the reference to the GameObject it’s attached to and use that to destroy the whole thing.