x


using sendmessage to destroy an object when an object is destroyed

I have a sphere that spawns robots and a cube that when it gets detroyed the sphere gets destroyed. I am using sendmessage to the sphere to destroy it but it isnt working. Here is the script for the cube:

var hitPoints = 100.0;
var detonationDelay = 0.0;
var explosion : Transform;
var deadReplacement : Rigidbody;

function ApplyDamage (damage : float) {
    // We already have less than 0 hitpoints, maybe we got killed already?
    if (hitPoints <= 0.0)
        return;

    hitPoints -= damage;
    if (hitPoints <= 0.0) {
        // Start emitting particles
        var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
        if (emitter)
            emitter.emit = true;

        Invoke("DelayedDetonate", detonationDelay);
    }
}

function DelayedDetonate () {
    BroadcastMessage ("Detonate");
}

function Detonate () {
    // Destroy ourselves
    Destroy(gameObject);

    // Create the explosion
    if (explosion)
        Instantiate (explosion, transform.position, transform.rotation);

    // If we have a dead barrel then replace ourselves with it!
    if (deadReplacement) {
        var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);

        // For better effect we assign the same velocity to the exploded barrel
        dead.rigidbody.velocity = rigidbody.velocity;
        dead.angularVelocity = rigidbody.angularVelocity;

        gameObject.SendMessage( "Destroy1",
        SendMessageOptions.RequireReceiver);
    }

    // If there is a particle emitter stop emitting and detach so it doesnt get destroyed
    // right away
    var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
    if (emitter) {
        emitter.emit = false;
        emitter.transform.parent = null;
    }
}

and the script for the sphere:

var maxrobots = 5;
var robot : GameObject;
var timebetweenspawns = 5.0;
var robotcount = 0;
var lastspawn =-10.0;
var explosion : GameObject;
var deadReplacement : Rigidbody;

function Destroy1 ()
{
    Destroy( gameObject );

    if (explosion)
        Instantiate (explosion, transform.position, transform.rotation);


    if (deadReplacement) 
    {
        var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);


        dead.rigidbody.velocity = rigidbody.velocity;
        dead.angularVelocity = rigidbody.angularVelocity;
    }
}

function Update () 
{
    if( Time.time > timebetweenspawns + lastspawn && robotcount <= maxrobots )
    {
        Instantiate( robot, transform.position, transform.rotation );

        lastspawn = Time.time;
        robotcount++;
    }
}
more ▼

asked Aug 25 '10 at 11:19 PM

MatthewGeorge gravatar image

MatthewGeorge
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

you'll want to declare a public variable in the sphere code (an int) and a variable in the cube code that is of type (whatever the name of your script for the sphere is) and before the destroy command, say "(shpere var).(cube var)--;, and set the if statement in the sphere code that if it equals 1, then destroy.

more ▼

answered Jan 14 '11 at 09:51 PM

zmar0519 gravatar image

zmar0519
946 59 66 78

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x764
x184

asked: Aug 25 '10 at 11:19 PM

Seen: 840 times

Last Updated: Aug 25 '10 at 11:25 PM