GMS 2 Editting the properties of an object created with instance_create

S

Spencer S

Guest
I have a hitbox I create in an attack state for the player. Here is my code:
Code:
            bodyHitbox = instance_create_layer(x + 450, y, layer, obj_body_hitbox);
            object_set_solid(bodyHitbox, true);
            bodyHitbox.mask_index = spr_player_attack_1_body_hitbox;
            bodyHitbox.sprite_index = spr_player_attack_1_body_hitbox;
            bodyHitbox.visible = true;
On the first line, I believe I store the ID of the instance created into the variable bodyHitbox. However, when I run the game, I get an error:
Execution Error - Variable references invalid object (-4).mask_index

I've created the bodyHitbox in the player Create event already, so I know the variable exists. I've also put a 1 step alarm on setting the mask_index, sprite_index, and visibility, so that the first line of code for sure could have time to store the ID of the created instance inside the variable bodyHitbox, but that doesn't work either.

The only solution I can think of is that it's not working because of some bug with running this kind of code in a script (I use state machines). I run an almost identical piece of code in the player Step Event itself, and it works just fine.

Or maybe the ID of the created obj_body_hitbox object isn't somehow being stored in the variable bodyHitbox? I'm stuck here.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
The only solution I can think of is that it's not working because of some bug with running this kind of code in a script (I use state machines). I run an almost identical piece of code in the player Step Event itself, and it works just fine.
I think a more likely cause would be that the object running the code was created at a depth rather than a layer, which would mean that layer variable has nothing meaningful inside of it, and thus instance_create_layer may decline creating an instance at an nonexistent layer.
 
Top