In a survival game, enemies die and respawn, but their positions still need to be updated every frame. An array stores enemy data by index: the index identifies which position belongs to which enemy. This lets the game iterate over data instead of following a reference to a separate object for every enemy. section 6.1.5 section 6.1.1
Arrays are a practical foundation for data-oriented design because their elements can be kept together in memory. A linked list spreads its nodes across heap memory, making traversal less local. C# List also uses an array internally, but its convenient operations do not guarantee the same access speed as using an array directly. In one Unity test that repeatedly incremented 10,000 elements, the array was 6–8 times faster than List. Treat that as a reason to measure the workload on target devices, not as a universal speed ratio. section 6.1.1 section 6.1.4
For enemies that enter and leave play, separate storage from membership:
- Preallocate an EnemyPosition array for the maximum number of enemies. An enemy’s index identifies its position throughout play. section 6.1.5 section 6.2
- Keep alive enemy indices in another array and track its used length with AliveEnemyCount. Iterate only that length, then use each stored index to access EnemyPosition. The count must not exceed the array’s allocated capacity. section 6.2
- Allocate a dead-indices array to the same maximum capacity. At game start, set the alive count to zero and fill the dead array with every available index in reverse order, ready to take indices from its end. section 6.5.2 section 6.5.3
Listing 6.4 Adding the alive enemy indices array to GameData
public class GameData
{
public int[] AliveEnemyIndices; #A
public int AliveEnemyCount; #B
public Vector2[] EnemyPosition;
public Vector2 PlayerDirection;
public float GameTime;
public float BestTime;
public MENU_STATE MenuState = MENU_STATE.NONE;
}The GameData listing shows the distinction: EnemyPosition holds data, while AliveEnemyIndices and AliveEnemyCount identify the portion of that data currently in play. section 6.2
This arrangement avoids checking an alive flag for every enemy during movement. As deaths and respawns make that flag’s pattern less predictable, such per-enemy branches can become costly. The extra arrays and counts are worthwhile when they remove work from a frequent loop; they also require deliberate capacity and membership management. section 6.1.5 section 11.7
Allocation lifetime matters too. Ordinary C# array contents are heap-allocated; repeatedly creating temporary arrays during Update can generate garbage-collector allocations. For per-frame added and removed enemy indices, Span<int> with stackalloc provides temporary arrays without heap allocation. For persistent enemy data, allocate arrays up front when a maximum size is known. section 3.2.1 section 6.6.1 section 6.5.2
Do not make an array the default for every game feature. A small main menu may not need arrays for cache-oriented iteration, though an array can organize references when it contains many elements. Where runtime dictionary lookups would occur frequently, assigning indices to data at tool time can instead enable array lookups; a dictionary may still serve occasional tasks such as loading saved data. section 8.2 section 10.8















































