GameMaker How to find specific object by ID

I currently have multiple objects of O_Car i want to find the specific object of O_Car that the player is colliding and not all cars so because of every object has a unique ID I find the ID of the object, however i do not know how to call it so i destroy the object

Code: Car
if(KeySpace) &&place_meeting(x,y,O_Player)
{
if(instance_exists(O_Player))
{
O_Player.CarSet = id;
}
}

Code: Player
instance_destroy(CarSet);

You can not destroy an object by its ID so how could you use the ID to specifically destroy the car which is being collided with?
 

Binsk

Member
Make sure you are reading the manual, namely this piece:
This function also accepts the special keywords all and other. Please note that should you need to get the unique instance id of the object being collided with, you should use instance_place.
So, instead of using place_meeting use instance_place, as said in the manual. This will return a special number (an ID) for the instance that was collided with. You can then use instance_destroy on that number instead of CarSet to destroy the one instance.

This also means that you will need to store the value in a temporary variable so that you can:
1) Check if there was a collision in the IF statement and
2) Delete the object tied to the ID
 

Yal

🐧 *penguin noises*
GMC Elder
In a collision event, destroy other (which is the other object involved in this collision).

In code,
Code:
with(obj_car){
  if(place_meeting(x,y,other){
     instance_destroy() //destroy the cars that hit us
  }
}
 

Yal

🐧 *penguin noises*
GMC Elder
Of course:
Code:
//Always works
with(the_id){
  instance_destroy()
}

//GMS2 only
instance_destroy(the_id)
 

FrostyCat

Redemption Seeker
You should learn the difference between an instance and an object in GM. You keep using the term "object" in this topic where you should have used "instance", and that is leading you to write all sorts of senseless, under-specific code. With the exception of functions containing instance_create or starting with object_, almost everything built into GML that takes an object ID can also take an instance ID to target a specific instance, including with blocks and instance variable dots (i.e. something.variable). Check the Manual before deciding that you have to put an object ID in something, and don't use object IDs to target instances if more than one instance of the object exists in the room.
 
Top