# Why is my variable seems to be non-existent in GameMaker?

I find myself stuck again with a peculiar issue that I don't really understand.

I'm currently trying to implement a "weapon" into a shooter game (using it as a learning project), as I was (and still am) a total noob in programming since two month ago.

Here is the code pointed by the error message. I'll explain what I'm trying to do right after.

  if instance_exists(Player)
{
nbtarget=instance_number(ENEMY);
var i;
for (i=0;i<nbtarget;i++)
{
Target= instance_find(ENEMMY,i)

if Target != noone
{
if Target.size <3 {hit=Target}
}
if hit != noone
{
{
repeat(2)
{draw_lightning(x, y, hit.x, hit.y,and other parameters)}

if damageTik = true
{
DMG = instance_create(hit.x,hit.y,Obj_dmg)
DMG.dmg = Current_dmg
}
}
}
}
}
damageTik= false;
}
else
{
instance_destroy()
}


So, the behavior I was looking for is a lightning that targets every enemy within a radius around the player, only if the enemy size is smaller than 3. Also, the targeted enemy takes a certain damage per second. Everything is working fine.

But, since I implemented that, when I test it, it works fine for a certain amount of time, and then I get this error:

FATAL ERROR in
action number 2
of Draw Event
for object lightning_weapon:
Unable to find any instance for object index '103752' name '<undefined>' at gml_Object_lightning_weapon_DrawEvent_2 (line 15) -if point_distance(x,y,hit.x,hit.y)<startradius


From what I understand, it looks like the instance referred to in the variable hit has been destroyed before running the code at line 15. But I'm checking if such instance exists just before (hit != noone), so how can this be? Any pointer will be appreciated.

I try to move the "instance_destroy()" part of the enemy code out from the Step Event (of the enemy parent object), I tried to move it in the End Step Event, then in the Begin Step Event, I even tried to make a variable to tell the enemy he is dead and destroy it in the next step... Nothing fixed my issue.

Maybe the problem is not with that part of the code, but after half a day looking at this I can't find any new thing to try.

I also try to do the same with a ds_list, but failed to make it work (only one enemy is hit by the lightning):

var target = ds_list_create();
with(ENEMY)
{
}

nbtarget = ds_list_size(target)
var i;
for (i=0;i<nbtarget;i++)
{
hit = ds_list_find_value(target,i)
if hit != noone
{
{draw_lightning(x, y, hit.x, hit.y,other parameters...)}
if fire = true
{
DMG = instance_create(hit.x,hit.y,ODamage)
DMG.dmg = dmg
}
}
}
fire = false;


If I manage to make one or the other to work, it's perfect.

• Where you declared hit? Also, it would be a bit better if you could make some proper indentation to your first code, the brackets are looking a mess. – DH. Jun 16 '16 at 16:39
• Took too long to edit, but just in case hit is the problem, try using target instead. – DH. Jun 16 '16 at 17:00
• Target.size <3 {hit=Target} hit is not the problem. i use hit and not target to get only the right size object – wintermute Jun 16 '16 at 18:07
• Instead of using hit inside your if, try using target. – DH. Jun 16 '16 at 19:23
• Messy code makes it hard to find out what's going on. You should really keep your code clean and well indented. Currently, there seems to be a missing if in your first code block. – Vaillancourt Jun 17 '16 at 15:14

Target= instance_find(ENEMMY,i)
But that's not correct, since your enemy objects are named ENEMY, not ENEMMY. Game Maker recognizes "ENEMMY" as a different Object Index, and tries to look for an object with that name. But it does not exist, and that's the reason your FATAL ERROR reads:
Unable to find any instance for object index '103752' name '<undefined>'
It is named '<undefined>' because it actually doesn't exists. Just fix this typo in your code, and everything will work fine. Or, at least, this error isn't going to show up anymore.