[SOLVED] Delete a SPECIFIC instance of an object out of many

Hello everyone.
In my game, I have an object that creates another object when the scene loads, For example, a background light, or an enemy with multiple pieces. For this example, I have an item that has a light object around itself that is spawned when the scene loads. This light object is also used for different items as well.
Expected result: When the player collects the item, delete the item AND it’s corresponding light.
Actual result: One of the netted results were that when the item is collected, ALL of the other lights around other items vanish as well instead of just the one around the collected item. Another result actually DID make the specific light disappear with the item, but if the same item is placed in a scene, that item does not spawn it’s own light. Only ONE of the specific items has a light while any others of the same item does not.

So here’s my question. How to I make it so that when an event is triggered, delete ONE SPECIFIC instance of an object out of many?

Thanks in advance.

1 Like

How does the player collect the item?
The way you pick the item to be collected and deleted should also work for the light.
For example
if player is in collision with item : delete item
if player is in collision with light : delete light

An other thing you can try is object linking:

When you spawn the light object at the position of the item, link the light to the item.
And then wen you collect the item, first take in to account the light linked to the item, delete the light and then delete the item.

How the item supposed to spawn it own light?
When you create the item you just need to create the light the same way.
Create object item at position X and Y
Create object light at position X and Y or at position item.X() and item.Y()

It is hard to tell only from a description what is wrong, we should see your events that you use to spawn items, lights and collect items.

1 Like

Okay. Here is a screenshot of how the items with lights work, and how the item spawns a light.

Everything you need is in the screenshot. This is the setup that deletes ALL the lights once one item is grabbed. You recommend that I should link the items?

I don’t understand anything even with a photo but I don’t know if you tried to set the conditions like this:

I really don’t know if it works sorry. :disappointed_relieved:

o maybe:

I suppose you can add an ID object variable to each Will and each WillLight to identify them. That way, when something happens to Will ID 1, you can make the same thing happen to WillLight ID1.


Yes. You check collision only with the item and your program have no way to know which light you are talking about. I can think of 3 ways to go about this. You can either also check collision with the light as I mentioned, or when you collide with the item, pick the nearest light to the item, or when you create the light in the for each loop, link it to the item using the object linking event, then when you collide with the item pick the light that is linked to the item, then delete the light and then delete the item.

1 Like

How use items id in the Gdevelop?

Thanks mate, I did the linking thing. After some experimentation, I eventually got it to work exactly how I imagined it. However, there is still one more problem. I decided to color the lights accordingly, based on the colors of their corresponding items. Whenever I place duplicates of the item (E.G. Two wills which both have Jump = 1), only the first one placed on the scene has a colored light. Once I grab the item, the second item’s light is now colored instead. Also, if I collect the second placed item before the first, the light doesn’t disappear. The light on the second item is only colored and disappears if, and only if the first one was collected. My assumption is that the first item is linked to it’s light, but not the second item, and the second item’s light only links if the first one is gone. Is it possible to have every duplicate of the item link to their lights? Thank you.

The ID of each item is actually just a variable that I named ID.
So basically, you can use variables to make IDs for items. E.G. If object variable ID = 1, do this. If variable ID = 2, do something else.
If you still need help, you can ask in the “How to…?” forums and somebody smarter than me will help you out.

1 Like

Sorry about the confusion mate. In my game, the item has several different variants determined by a variable called ID. Each ID grants a different effect, and each different ID is tinted a different color. I really didn’t want to make a separate item for each effect, so I did this.

1 Like

Gonna bump this up to keep it relevant.

Has this not been solved?

No, it’s still not solved. Everyone just stopped commenting after my latest reply to ddabrahim.

try this code:

Thanks for the code, but is there a more convenient option?
The Will object hovers up and down to make it’s movements more realistic, and I would like the light to be attached to it. Basically, I would like to be able to place the Will object in the scene, and when the preview runs, each and every Will object will automatically spawn one light behind itself, each light will be positioned behind each Will object no matter where they move, and when the Will object is collected, delete ONLY the light that is associated with the Will object that was collected.
I’m sorry if I’m making this too confusing. Perhaps what I’m trying to accomplish is too complicated for GDevelop to handle. :frowning:

When you use a condition only the objects affected by the condition will be affected by the actions. So if you check for collision with will, and then delete object will, only the one touched will be deleted. Use linked objects to link the light with every will object on creation of the lights. Then add take in consideration linkned objects and use delete the light object before deleting the will object.

1 Like

Looking at your last screenshot, you need to use a for each event for every color change event, because you want it to apply to several instances.
And the collision sub-event that you duplicated under each one can be moved to a separate event because it’s the same event for all types of Wills, so one is enough.

1 Like

Very sorry for the late reply, but…
I put For Each events where you said to put them and IT ACTUALLY WORKED.
Every will spawns with the correct color light, and each will deletes ONLY the light corresponding to it.
Thank you mate. This will probably not only help with this problem I had, but this can potentially make other things easier for me (E.g. Enemies with multiple body parts, segmented “worm” enemies, etc.)

Again, thanks for the help. It finally works exactly like I imagined it. I’ll let you know if anything goes wrong with this topic, but as of now, it is solved.


with the help of ID i made a mechanic that allows u to delete the objects that u’ve created one by one by clicking on them