Object destroys itself even with instance_destroy(other)

M

Mael

Guest
Hello there!

I am working on a big project for quite a time now, so I am pretty familliar with gamemaker now. Everything works fine until I try to add a coin system. Here the explanation:

I have an obj_ball that you can flick. I also have an obj_coinBrown.
In the obj_ball step event I coded this:
// With a brown coin
if (place_meeting(x, y, obj_coinBrown))
{
with other instance_destroy();
global.coins += 1;
drawCoins = true;
alarm[0] = room_speed * 3;
}

All of this works fine, but my game crashes when colliding with a coin because the obj_ball gets destroyed too. I tried all different methods aswell but either it doesnt detect the collision or the ball gets destroyed again. For example I tried getting the ID of the coin that collides but no change of results.

Can someone please explain to me why my obj_ball gets destroyed over and over again? :(
Thank you very much!
 

FrostyCat

Redemption Seeker
place_meeting() is NOT a genuine collision event. It will NOT set the identity of other. You aren't familiar with GM if you still fall for this rookie myth.

If you need the colliding instance ID, use instance_place() instead of place_meeting().
Code:
var inst_coinBrown = instance_place(x, y, obj_coinBrown);
if (inst_coinBrown != noone) {
  with (inst_coinBrown) instance_destroy();
  global.coins += 1;
  drawCoins = true;
  alarm[0] = room_speed*3; 
}
 
M

Mael

Guest
place_meeting() is NOT a genuine collision event. It will NOT set the identity of other. You aren't familiar with GM if you still fall for this rookie myth.

If you need the colliding instance ID, use instance_place() instead of place_meeting().
Code:
var inst_coinBrown = instance_place(x, y, obj_coinBrown);
if (inst_coinBrown != noone) {
  with (inst_coinBrown) instance_destroy();
  global.coins += 1;
  drawCoins = true;
  alarm[0] = room_speed*3;
}
Thanks, it worked. Excuse me, I didnt want to brag or anything. Thank you again for teaching me this. :)
 

Yal

šŸ§ *penguin noises*
GMC Elder
Also, just as a tip for any future issues you run into: if you get weird results when using a function, check its manual page (place the cursor over its name and press F12 to instantly open it) to make sure it does what you think it does.
 
P

ParodyKnaveBob

Guest
Howdy, Mael,

Alternatively, you could move this to a proper Collision Event. Depends on your setup of course. (Btw, tip: wrap your code blocks in [code]example[/code] BB code to keep your indentation and such!)
Code:
with (other) instance_destroy();

++global.coins;
drawCoins = true;
alarm[0] = room_speed * 3;
Done. $:^ J
(I'm really not sure why people cram so much into the Step Event, frankly.)

I hope this helps,
 
Top