Creating a fireball; removal problems

Hello all,

I have been working on a fireball script for a day or so now. I've gotten it nearly exactly how I want it to work, but I've run into a problem where the explosion that I create doesn't go away.

The log complains of a NullReferenceException on line 12 of the following code:

var explosion : GameObject;

    function OnCollisionEnter( collision : Collision )
    {
        gameObject.GetComponent(Rigidbody).velocity = Vector3(0,0,0);  //set missile's velocity to 0 so it doesn't move and make more explosions (or not, for a multi-explosion missile)
        var contact : ContactPoint = collision.contacts[0];
        var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
        var instantiatedExplosion : GameObject = Instantiate(explosion, contact.point, rotation );
        var core = transform.Find("Core");  //find the missile's child, "Core"
        var streams = transform.Find("Streams");  //find the missile's child, "Core"

        core.GetComponent(ParticleEmitter).emit = false;  //turn off the core's emision

        core.transform.parent = null; //set core's parent to null
        streams.GetComponent(ParticleEmitter).emit = false;  //turn off the core's emision
        streams.transform.parent = null; //set core's parent to null
        instantiatedExplosion.transform.Find("Streams").GetComponent(ParticleRenderer).velocityScale = 5.0;
        yield WaitForSeconds(0.2);
        instantiatedExplosion.transform.Find("Streams").GetComponent(ParticleRenderer).velocityScale = 0.5;
        yield WaitForSeconds(1);
        instantiatedExplosion.transform.Find("Streams").GetComponent(ParticleEmitter).emit = false;
        instantiatedExplosion.transform.Find("smoke").GetComponent(ParticleEmitter).emit = false;
        instantiatedExplosion.transform.Find("OuterCore").GetComponent(ParticleEmitter).emit = false;
        Destroy (instantiatedExplosion.gameObject, 10);
        Destroy(core.gameObject, 3);
        Destroy(gameObject); //make sure this goes at the end, otherwise the script will be killed and deleted next frame
        //instantiatedExplosion.transform.Find("OBJECT").GetComponent(ParticleEmitter).maxEnergy //if we want to grab the max particle life for use in a yield
    }

Often times this happens when my missile object makes a second collision before being deleted, and it's the second collision's explosion stays.

I'm pretty stumped on this one; any help would be appreciated!

EDIT: After more thought, I find it a little odd that my missile object travels so far to create a secondary explosion before being destroyed. See below:

alt text

Perhaps there is a more to this than it seems. Guesses, answers, suggestions?

Also, I've added some code as per Statement's suggestion, and this doesn't help

function OnCollisionEnter( collision : Collision )
{
    if (!gameObject)
        return;
    gameObject.GetComponent(Rigidbody).Sleep();  //set missile's velocity to 0 so it doesn't move and make more explosions (or not, for a multi-explosion missile)
    ....
}

Cheers,

Unislash

The fix to this flavor of problem is to check if your object is null at the beginning of the function (as said by Statement above).

In my particular case, the following code is sufficient to fix the problem:

function OnCollisionEnter( collision : Collision )
{
    if (transform.Find("Core") == null || transform.Find("Streams") == null)
        return;
    ...
}

Note that "if (transform == null)" will not solve the problem.

Thank you very much for your help guys :-)