Suppose a designer sets the number of enemies to zero. If the game checks that value every time it allocates enemy arrays, a data-entry mistake adds branches to runtime code. Validate the balance before parsing it instead: reject the bad value during tool time, then let allocation use the checked data. section 9.4 section 9.3
Keep the boundary explicit:
- Load the editable balance asset and check constraints that matter, such as an enemy count greater than zero and no higher than its allowed maximum. section 9.4
- Throw an exception on failure so invalid data is not parsed or saved. Call validation before parsing. section 9.4
- Check assigned sprites, prefabs, or other references when missing assignments cause problems. Avoid an exhaustive check of every field without a reason. section 9.4
Listing 9.8 A function to validate our data before parsing
public static void validate()
{
BalanceSO balanceSO =
(BalanceSO)AssetDatabase.LoadAssetAtPath(
"Assets/Data/Balance.asset", typeof(BalanceSO));
if (balanceSO.NumEnemies <= 0)
throw new System.InvalidOperationException(
"NumEnemies needs to be bigger than 0! Currently set to " +
balanceSO.NumEnemies);
if (balanceSO.NumEnemies > 10000)
throw new System.InvalidOperationException(
"NumEnemies is set to " + balanceSO.NumEnemies +
". Max is 10000");
}The Unity editor validation function loads a balance ScriptableObject and rejects enemy counts outside the permitted range before the balance is parsed. section 9.4
Validation belongs at boundaries, not repeatedly inside a controlled update loop. Loading disk or server data, parsing files, handling user input, and accepting editor-assigned references can all warrant checks. Once an assumption has been established, investigate its cause if it later breaks rather than hiding the bug behind another early return. Moving checks out of hot code can simplify it and avoid unnecessary branches; it does not make every runtime check disposable. section 11.2 section 9.7











































