game-maker-studio-2

How to destroy "the other" instance in Game Maker 2.0


I'm using Game Maker 2.0.

I'm generating instances of an helicopter in the Control object as this:

if ( !global.helicopter_wait && global.num_helicopters <= global.max_helicopters )  
{
    instance_create_layer(800,200,"inst_game",obj_helicopter);
    global.num_helicopters++;
}

Then, in there is a missil object where I've put the following code in the Step event:

if ( place_meeting(x+hspeed,y+vspeed,obj_helicopter) ){
    instance_destroy();
    instance_destroy(other);
    global.num_helicopters--;
    global.score += 10;
}

The problem is that the missil instance is destroyed but the helicopter's one is not. Also, I've tried to do the same with the the collision event, but it does not work either.

How can I do this correctly?


Solution

  • Remove the first instance_destroy(); (or put it as the last line of the code within the check).

    if ( place_meeting(x+hspeed,y+vspeed,obj_helicopter) ){
        instance_destroy(other);
        global.num_helicopters--;
        global.score += 10;
        //instance_destroy(); //activate this line if you want to remove the missile as well
    }
    

    Once instance_destroy(); is triggered on itself, it won't continue the code within the object past that line.

    There's also a function that counts all existing objects in the current room, that could save you some trouble keeping track of the global.num_helicopters. For that, you could take a look at instance_number