Unable to find instance?

M

Mandala

Guest
I'm attempting to create something similar to a teleport ability in a game, but this keeps happening after completing the function.
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_Player1:

Unable to find any instance for object index '0' name 'obj_Player1_shadow'
at gml_Object_obj_Player1_Step_0 (line 28) - obj_Player1.x = obj_Player1_shadow.x
############################################################################################


This is the code i'm using to perform the function. Any help is appreciated.
if (keyboard_check(vk_shift)){
obj_Player1_status.Player1energy -= 2
obj_Player1.x = obj_Player1_shadow.x
obj_Player1.y = obj_Player1_shadow.y
instance_destroy(obj_Player1_shadow)
}
 

jo-thijs

Member
I'm attempting to create something similar to a teleport ability in a game, but this keeps happening after completing the function.
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_Player1:

Unable to find any instance for object index '0' name 'obj_Player1_shadow'
at gml_Object_obj_Player1_Step_0 (line 28) - obj_Player1.x = obj_Player1_shadow.x
############################################################################################


This is the code i'm using to perform the function. Any help is appreciated.
if (keyboard_check(vk_shift)){
obj_Player1_status.Player1energy -= 2
obj_Player1.x = obj_Player1_shadow.x
obj_Player1.y = obj_Player1_shadow.y
instance_destroy(obj_Player1_shadow)
}
Hi and welcome to the GMC!

The code you posted shouldn't even compile, as this line has an invalid amount of arguments:
Code:
instance_destroy(obj_Player1_shadow)
It should instead be:
Code:
with obj_Player1_shadow
    instance_destroy();
The error message you're getting is saying that there's no instance of type obj_Player1_shadow active in the room when you're executing that code.
I presume you're executing this code, it runs fine and destroys obj_Player1_shadow and then you execue it again.
You should add an extra check (such as instance_exists(obj_Player1_shadow)) next to keyboard_check(vk_shift).
 
The code you posted shouldn't even compile, as this line has an invalid amount of arguments:
Code:
instance_destroy(obj_Player1_shadow)
It should instead be:
Code:
with obj_Player1_shadow
instance_destroy();
Incorrect, although you can do as you show in your second code block, as this is tagged as GML2 that would mean that the OP is using GMS2 - and the instance_destroy function now allows 2 optional parameters:
https://docs2.yoyogames.com/index.h...nces/instance_functions/instance_destroy.html
You can now pass in an instance id or an object index as the first parameter to force it to destroy a specific object/instance, rather than the calling instance.
@Mandala You need to confirm whether there is in fact an instance of the obj_Player_shadow, just as @jo-thijs mentioned by using instance_exists before attempting to read anything from it.
 

jo-thijs

Member
Incorrect, although you can do as you show in your second code block, as this is tagged as GML2 that would mean that the OP is using GMS2 - and the instance_destroy function now allows 2 optional parameters:
https://docs2.yoyogames.com/index.html?page=source/_build/3_scripting/4_gml_reference/instances/instance_functions/instance_destroy.html
You can now pass in an instance id or an object index as the first parameter to force it to destroy a specific object/instance, rather than the calling instance.
@Mandala You need to confirm whether there is in fact an instance of the obj_Player_shadow, just as @jo-thijs mentioned by using instance_exists before attempting to read anything from it.
Ah, thanks for the info!
 

TheouAegis

Member
I think the issue may be because you're using keyboard_check(vk_shift) and then destroying the shadow. So on the next step the player is still holding the shift key because he hasn't let go of it yet and a shadow hasn't been recreated yet, so it comes up missing. You should probably be using keyboard_check_pressed() and then debug further from there.
 

jo-thijs

Member
I think the issue may be because you're using keyboard_check(vk_shift) and then destroying the shadow. So on the next step the player is still holding the shift key because he hasn't let go of it yet and a shadow hasn't been recreated yet, so it comes up missing. You should probably be using keyboard_check_pressed() and then debug further from there.
You're correct about the cause of the issue, the discussion above already pointed that out,
but the solution you propose is not a complete solution.
Sure, using keyboard_check_pressed will fix the issue if the player only presses the key once for the duration of the teleportation animation,
but if the player presses the key twice in that duration, you would still get an error.
Some check to make sure the teleportation animation isn't running is required.
Using instance_exists(obj_Player1_shadow) is an easy way to do this.
 

sercan

Member
maybe you missed to put obj_Player1_shadow object in the room.
or maybe after destroying obj_Player1_shadow, you push shift again. to do that, you need to bring the obj_Player1_shadow back to the room.
 
Last edited:
Top