GML Bullets not always hitting enemy

W

Wolfrick

Guest
Whenever the obj_leftBullet_shooter hits the enemy obj_water, the enemy is supposed to die in one hit. It works fine until I code in instance_destroy(); for the obj_leftBullet_shooter and I don't know why. If I code it so the bullet disappears once it hits the enemy, sometimes it'll take multiple hits before the enemy gets destroyed. This only happens once I've coded in the instance_destroy(); for the bullet.

Current Code:

obj_water, [event] collision with obj_leftBullet_shooter

Code:
///Collision Effect

//Die when hit by bullet

with(obj_water){
    obj_water.hp -= 100;
}

instance_destroy();
obj_water [event] step event

Code:
//Death
if(hp == 0){
    instance_destroy();   
}
obj_leftBullet_shooter, [event] collision with obj_water

Code:
///Destroy

instance_destroy();


 

Ratsha

Member
//Die when hit by bullet with(obj_water){ obj_water.hp -= 100; }
In the collision event you can refer the keyword other, to the instance you're colliding with. You are currently taking ALL instances of obj_water and subtracting 100 hp. Instead you can simply write:

Code:
///Collision Effect

//Die when hit by bullet

other.hp -= 100;
instance_destroy();
 
W

Wolfrick

Guest
I just get this error whenever I use other

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Eventobj_leftBullet_shooter
for object obj_water:

Variable <unknown_object>.<unknown variable>(100002, -2147483648) not set before reading it.
at gml_Object_obj_water_Collision_319995a3_8e03_44d1_9255_c897bbe70569 (line 5) - other.hp -= 100;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_water_Collision_319995a3_8e03_44d1_9255_c897bbe70569 (line 5)
 

Ratsha

Member
I just get this error whenever I use other

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Eventobj_leftBullet_shooter
for object obj_water:

Variable <unknown_object>.<unknown variable>(100002, -2147483648) not set before reading it.
at gml_Object_obj_water_Collision_319995a3_8e03_44d1_9255_c897bbe70569 (line 5) - other.hp -= 100;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_water_Collision_319995a3_8e03_44d1_9255_c897bbe70569 (line 5)
Oh, I assumed it was your Collision Event. Please let us review the collision detection in the step event as well, that's where it seems to go wrong.
 
W

Wolfrick

Guest
Oh, I assumed it was your Collision Event. Please let us review the collision detection in the step event as well, that's where it seems to go wrong.
I was following a tutorial online and there wasn't any code for the collision in the step event only this:

Code:
///Die & Follow Player

//Death
if(hp == 0){
    instance_destroy();   
}

//Call Script
scr_enemy_follow()
 

Ratsha

Member
Put this code in the Collision Event with obj_water instead of step event:
Code:
///Collision Effect

//Die when hit by bullet

other.hp -= 100;
instance_destroy();
 
Top