An artist's depiction of an Extended Unit Death
Yeah, if you cannot accurately figure out the unit index, then you're kinda dead in the water. This almost always requires the unit to be preplaced.
Quote from name:Azrael.Wrath
Or if all units are always created in the same order.
and "uncreated" aka die, killed or removed.
Supposing that you can keep track of all the units created/destroyed, you still have the problem of having to create the triggers for all of the IDs that the unit could be.
There seems to be a lot of discussion on this, so I'll clarify:
The Index ID is determined based on when the unit is placed on the map. Assuming no units have died, this means that the ID will increment upwards, starting from 0 (which is the first unit placed on the map). Start locations DO NOT get assigned an Index ID (SCMD2 incorrectly does this). To make your Index list accurate, place the Start Locations
after all units you're checking indexes for. Start Locations appear to be the only exception (units like Map Revealers, Scanner Sweeps, etc. have Index IDs).
The way the next index is assigned to a unit is as follows: As soon as that unit is either in production or has otherwise appeared on the map, they are given an Index ID. This Index ID is not released until the unit's presence on the map is gone. After a unit comes into existence, SC performs some logical test to find the next index. THIS INDEX IS NOT ALWAYS PREDICTABLE.
For example:
1) The Index Queue is set to 0 at the beginning of the game (no units exist yet).
2) A unit is created. Its index gets set to the Index Queue's current value (0).
3) SC finds a new available index (1).
4) The Index Queue is set to the available index (1).
5) 50 more units are created following this pattern. They each have a unique index from 1 to 50.
6) SC finds a new available index (51).
7) The Index Queue is set to the available index (51).
8) All 51 units on the map are removed via triggers.
9) A unit is created. Its index gets set to the Index Queue's current value (51).
10) SC finds a new available index (0).
11) The Index Queue is set to the available index (0).
In the above example, at step 9, even though there were no units on the map, and thus index 0 is available, the next unit to be placed gets assigned index 51 before SC looks for a new available index. In other words, the next index is predetermined, and it is calculated immediately after some unit is assigned an Index ID.
Now, that was all based on observation and hypothesis. To be honest, I am unsure exactly how SC assigns indexes. Attached to this post is a map you can experiment with if you want to try and find a pattern. It almost seems like SC is using some strange variation of the Quick Sort algorithm, as you may notice that when creating/killing units very quickly, it begins to alternate between two polarized points in the list.
TL;DR: Just preplace the damn units.