Destructible Buildings

I have been building a game prototype that has destructible buildings spread across a large city environment. The problem I am having is writing the script for the smaller parts of the building, that when destroyed will subtract from the buildings over all health. When the over all health reaches zero it is replaced with a model that has a collapsing animation.
My question is does anyone have any ideas on how I can write a script that takes damage done to an object and when it reaches zero it swaps out the object for a particle effect and then sends a value or function command to the script that controls the buildings base structure so that it will subtract from the whole of the building health and cause it to fall down into a pile of rubble that has been pre-animated?
I have objects that take damage and swap out for a particle effect leaving a big open hole in the wall it’s just getting all of those removed objects to subtract from the larger structure so it will fall when enough parts destroyed.

Any Ideas?

Thanks

Hi, welcome to the forum!

A good way to handle this would be to make all the pieces of the building children of a main parent object that represents the building. You can then call a function on the parent using SendMessage:-

transform.root.SendMessage("FunctionName");

You could make a prefab to produce the particle effect for the explosion. If you put it in a folder called Resources, you can load the prefab on demand using Resources.Load. The particle animator component of a particle system has an autodestruct property that you can use to destroy the object after the effect has ended. Your code on the piece of building might look something like:-

if (hitPoints == 0) {
   Object.Instantiate(Resources.Load("Explosion"), transform.position, Quaternion.identity);
   Destroy(this.gameObject);
}