If you have enough discrete rate of gathering and spending this resource (you know 100, 200, 300, ... not 100, 101, ... , 199, 200 or 1, 2, 3, 4, ... would work too) you could make a dummy unit for each amount of resource. The max of these dummy units will be the cost of most expensive unit, then you store the value in a variable, so you do not need too much these dummy units. Their name will correspond to their value (e.g. "200 gold"). Depending how much you spend and gather, you remove/add that many gold units. Now when you will like to train/build/research stuff using this resource, the button will be inactive as long you have too few resources.
Hope you understand.