Here's a slightly different version of my previous answer about this kind of system.
The big idea is to move away from using differently-named variables for each resource, because then we need to write different code to access each one. Instead, we can treat them uniformly as members of a collection, indexed/keyed by a resource type.
First we define our types (we could do this in data too via a Type Object, if you want to be able to add new resources dynamically without re-compiling, eg. for mods & expansions)
public enum ResourceType : int {
Food,
Wood,
Iron,
Stone,
// Ensure this entry is last and it will automatically keep track
// if how many resources we have. (If we don't explicitly assign
// values anywhere to confuse the auto-numbering)
TOTAL_COUNT
}
Then we define a resource holder that can process transactions on each of these resource types as needed:
public class ResourcePool {
uint[] _resources = new uint[(int)ResourceType.TOTAL_COUNT];
public int GetQuantity(ResourceType type) {
return _resources[(int)type];
}
public void Add(ResourceType type, uint quantity) {
_resources[(int)type] += quantity;
}
public bool TryDeduct(ResourceType type, uint quantity) {
if(GetQuantity(type) < quantity)
return false;
_resources[(int)type] -= quantity;
return true;
}
}