I think you mean something like this:
On object2 created - object1 add to variable value = object2.value
On object2 destroyed - object1 subtract from variable value = object2.value
With multiple objects it would probably be easiest to use the family feature and have the adding event be in a function.
Maybe using the is on screen - condition, would be an option.