• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker How to destroy one instance of an object.

O

Oxiriark

Guest
I am very new to Game Maker Studio, and I've learned a ton over the past week from tutorials.
So, I am creating a simple Mario platformer game. I added coins and basically this is my code for them currently.
Code:
if (place_meeting(x, y, obj_player))
{
    instance_destroy(obj_coin, false);
    audio_play_sound(snd_coinGrab, 0, false);
}
this of course destroys any instance of the object, so if I touch one then all will disappear.
could I get some help with this?
 

Binsk

Member
The function place_meeting checks if two instances's bounding boxes intersect and returns true or false. The function instance_place does the exact same check but returns the id of the first instance that is intersected. instance_place_list does the same check as well but returns a number of instances in that location.

Assuming you don't have coins that overlap you can use instance_place as follows:
Code:
var _instance = instance_place(x, y, obj_player); // Do the check, store result in temporary variable
if (_instance != noone) // If the check didn't return "no one" we have an ID stored
{
    instance_destroy(_instance, false); // Destroy just the ID of our checked instance
    audio_play_sound(snd_coinGrab, 0, false);
}
You can read more about this function (and related functions) here and here.

Edit: Linked the wrong manual entry. Fixed.
 
Last edited:
O

Oxiriark

Guest
I attempted to implement this code, and the result seems to delete the player object instead of the coin object.
 

Binsk

Member
Ah, apologies. I read through your code too fast and assumed the player was doing the collision checking against the coins, not the other way around. Didn't even read the name of the object being checked.

In this case your original code was fine with the exception of you destroying 'obj_coin'. Each instance has a unique id that you can specify in order to modify the one instance. You can access this id via the local variable id. Pretty straightforward. Instead of specifying to destroy obj_coin simply specify to destroy the instance's id and it will only destroy the one.
 
Top