From Test-Scratch-Wiki
There may come a time when a user may need to assign an arbitrary number of sprites a unique identifier. For instance, say the user has ten bullet sprites with a local variable. Rather than individually assigning each of them a number in a set () to () block, it might be handy to do this automatically. That way, a script could specifically call bullet number seven without hard-coding it.
Firstly, a global variable (sprite count
is created. Then, on the stage, this script is added:
when gf clicked set [sprite count v] to [0] broadcast [count sprites v] and wait . . .
Now, for all the sprites that want to be counted (and generally these will be clones of the same sprite) this script is added:
when I receive [count sprites v] change [sprite count v] by (1) set [my number v] to (sprite count) //Swap these two blocks to start count at 0 instead of 1
Then, when the flag is clicked, each sprite will obtain a unique number in (my number
.
This works because of the execution order of broadcasts.