instance_destroy(obj_enemy) doesn't work

Cffex

Member
Code:
//Shoot config
if (global.lifes > 0) //if player not dead
{
if (global.lifes < 1) //if player dead
{
dmg = 0;
dmg2 = 0
instance_destroy(obj_enemy); //ERROR
}
if (global.lifes > 0)//if player not dead again
{
direction = point_direction(x,y,obj_player.x,obj_player.y);
speed = 4;
image_angle = direction;
}
}
 
What kind of error are you getting? I'm guessing you're getting an error because the object doesn't exist??

If so, change your instance_destroy() line to:

if instance_exists(obj_enemy) then instance_destroy(obj_enemy);


Hope that helps, let me know if that doesn't help.. and if it doesn't please let me know what kind of error you are getting.


Also, just a heads up.. your code is going to cause you problems as your game gets more advanced..

You probably don't want to kill the enemy object just because the player died. That seems silly to be honest, but I don't know what kind of game you are making either. In most situations, that would be really silly.

My suggestion to you is to instead have code in the enemy object to keep track of it's own health and destroy itself when it's health goes below zero.

So for example let's say that as your game develops more, your player starts out with a regular bullet type and it will kill that enemy object, but maybe in time you add a ice bullet (just an example) but you decide that because this ice bullet fire much faster and all that you want that enemy object to take 2 or 3 shots to be killed by the ice bullet. So in the collision with the regular player bullet in the enemy object collision check with it, you might have 50 damage be done. Let's also pretend that you've set the enemy object in its create event to have a "my_health = irandom_range(35, 45)" line that sets it to have a random health amount between 35 and 45. So we know that the players first bullet, just one hit should destroy it. You have a line of code that simply checks to see if its "my_health" has dropped to zero or below as in "if my_health <= 0 then instance_destroy();" Again that check needs to be in its step event.

Now, for the ice bullet you added in my example, your enemy object would have a collision check with that and code that perhaps says "my_health -= 15" or whatever you want to set the damage at. Now when your enemy object takes hits from 3 ice bullets, it should be dead. Want it to take randomly between two or three hits from ice bullets to die? Easy, just increase that damage in the collision with the ice bullet to 20 damage!

..I hope that makes sense.
 
Last edited:

Mr Magnus

Viking King
Think about this like a computer:


GML:
if(global.lifes > 0){ //Lives is higher than zero.
    if(global.lifes < 1){ //We've already established lives is higher than zero, but now is it less than one?
        //Lives must be between zero and one.
    }
}
Is there ever a situation where the player can have a fractional amount of lives? Likely not, so that code will never run.

Same with the second if statement: you don't need to check if lifes is larger than zero a second time in a block that only runs if that exact statement is true.
 

Cffex

Member
Think about this like a computer:


GML:
if(global.lifes > 0){ //Lives is higher than zero.
    if(global.lifes < 1){ //We've already established lives is higher than zero, but now is it less than one?
        //Lives must be between zero and one.
    }
}
Is there ever a situation where the player can have a fractional amount of lives? Likely not, so that code will never run.

Same with the second if statement: you don't need to check if lifes is larger than zero a second time in a block that only runs if that exact statement is true.
So i should do this?
//Shoot config
if (global.lifes > 0) //if player not dead
{
direction = point_direction(x,y,obj_player.x,obj_player.y);
speed = 4;
image_angle = direction;
}

if (global.lifes < 1) //if player dead
{
dmg = 0;
dmg2 = 0
instance_destroy();
}
 

Cffex

Member
What kind of error are you getting? I'm guessing you're getting an error because the object doesn't exist??

If so, change your instance_destroy() line to:

if instance_exists(obj_enemy) then instance_destroy(obj_enemy);


Hope that helps, let me know if that doesn't help.. and if it doesn't please let me know what kind of error you are getting.


Also, just a heads up.. your code is going to cause you problems as your game gets more advanced..

You probably don't want to kill the enemy object just because the player died. That seems silly to be honest, but I don't know what kind of game you are making either. In most situations, that would be really silly.

My suggestion to you is to instead have code in the enemy object to keep track of it's own health and destroy itself when it's health goes below zero.

So for example let's say that as your game develops more, your player starts out with a regular bullet type and it will kill that enemy object, but maybe in time you add a ice bullet (just an example) but you decide that because this ice bullet fire much faster and all that you want that enemy object to take 2 or 3 shots to be killed by the ice bullet. So in the collision with the regular player bullet in the enemy object collision check with it, you might have 50 damage be done. Let's also pretend that you've set the enemy object in its create event to have a "my_health = irandom_range(35, 45)" line that sets it to have a random health amount between 35 and 45. So we know that the players first bullet, just one hit should destroy it. You have a line of code that simply checks to see if its "my_health" has dropped to zero or below as in "if my_health <= 0 then instance_destroy();" Again that check needs to be in its step event.

Now, for the ice bullet you added in my example, your enemy object would have a collision check with that and code that perhaps says "my_health -= 15" or whatever you want to set the damage at. Now when your enemy object takes hits from 3 ice bullets, it should be dead. Want it to take randomly between two or three hits from ice bullets to die? Easy, just increase that damage in the collision with the ice bullet to 20 damage!

..I hope that makes sense.
the object do exists, i checked
 
No @ cffex

You're not understanding what he is saying at all.

If your player isn't dead it's lives isn't going to be there because of your if lifes <1 check to instance_destroy it

take your check for if lives > 0 out completely


//Shoot config

direction = point_direction(x,y,obj_player.x,obj_player.y);
speed = 4;
image_angle = direction;

if (global.lifes < 1) //if player dead
{
dmg = 0;
dmg2 = 0
instance_destroy();
}
 

Cffex

Member
So i should do this?
//Shoot config
if (global.lifes > 0) //if player not dead
{
direction = point_direction(x,y,obj_player.x,obj_player.y);
speed = 4;
image_angle = direction;
}

if (global.lifes < 1) //if player dead
{
dmg = 0;
dmg2 = 0
instance_destroy();
}
I forgot to tell ya something, i wanna destroy the enemy, but the code isn't in the enemy object what should i do???
 

TsukaYuriko

☄️
Forum Staff
Moderator
You also forgot to tell us the error message, as in the most important part. You don't expect your doctor to tell you what's wrong if all you tell them is that it hurts, either, right?

Don't leave us guessing. Tell us what only you can know, because we can't know unless you do.


the object do exists, i checked
How did you check it?
 

Cffex

Member
You also forgot to tell us the error message, as in the most important part. You don't expect your doctor to tell you what's wrong if all you tell them is that it hurts, either, right?

Don't leave us guessing. Tell us what only you can know, because we can't know unless you do.



How did you check it?
Wrong number of arguments to functions or script
instance_destroy(obj_enemy <-- wrong argument thing)
 

TsukaYuriko

☄️
Forum Staff
Moderator
Then you're trying to use syntax that is not supported in your version. Please specify that you're using an old version whenever you ask for help - it makes a lot of situations very easy to solve (like this one).

instance_destroy did not always support passing in what should be destroyed.
In old versions, use a with statement to change execution scope to what's supposed to be destroyed, then call instance_destroy(); on it.
 

Cffex

Member
Then you're trying to use syntax that is not supported in your version. Please specify that you're using an old version whenever you ask for help - it makes a lot of situations very easy to solve (like this one).

instance_destroy did not always support passing in what should be destroyed.
In old versions, use a with statement to change execution scope to what's supposed to be destroyed, then call instance_destroy(); on it.
Okay!
 
Top