delete last created object?

I’ve got a script that will create a new empty wherever and whenever I right click. I have another script that tells an object (“test object”) to move towards the last created empty. for some reason I can’t get the old empties to be removed. all the empties have the same name (“target”), which is a problem when using endObject(). they have different ID’s, but I’m having trouble deleting objects by their ID.

any ideas?

[SOLUTION]

the empties were getting deleted, but I was creating them faster than I was deleting them. every time you have something like “addedEmpty = scene.addObject(empty, spawnPoint, 100)” it creates an empty and assigns it to to a variable. I thought it just assigned the last one to a variable. my code looked something like this:

scene.addObject(empty, spawnPoint, 100)
addedEmpty = scene.addObject(empty, spawnPoint, 100)

whenever creating a new object assign it to a variable…
Like:

addedEmpty = scene.addObject(empty, spawnPoint, 100)

then the id of that object is stored into the variable… so you can delete it by:

addedEmpty.endObject()

if you want to handle multiple objects then you would have to append the variable to a list each time it is added and then use a for loop on that list to delete all the empties except the last one…

I think this is your problem… Maybe you should post the .blend or the scripts so that we can understand what you really want…

I’m 99% sure I found the problem. The explanation is long, so I will explain in the morning (it’s past my bed time).

Edit:

thanks, your reply helped me figure out what was wrong.

Sidenote: Why do you create a new empty if you just need one? You could simply move the one that exist.

Another way is to remove all empties first and add the new one afterwards.

Does this have to do with a voxel game or something like it?

I’m working on an RTS. each “unit” needs it’s own empty to serve as a target to track and move to. I programmed the “units” to always move towards their respective target empty. once they get to said empty, the empty get’s deleted and the “unit” stops moving.

As you say the have their own designated target, you can simply end this target object. It is already known - no need to look through the phonebook to find one with the same name ;).

I hope you understand what I mean.