Setting up smart damage taking in game...

I'm trying to set up a game like Phage Wars.

In that game when your troops collide they either support (if it's your building) or attack (if it's the enemies building) the buildings.

I want to know how to set up a way to tell whether to add or subtract to the number of people when a soldier collides a building. How would I do that?

(This depends on whether you control or they control the building and which team the soldier is on).

You would use a standard collision script to detect collisions - if you haven't got that in place yet, I suggest you search it.

You'd then have an if statement inside the function called on any soldier-building collision.

You'd check if the building collider is owned by you or the enemy - you'll have to set a variable each time a building is captured.

If it's yours the soldier will run a function to add to the variable, if it's the enemies you'd run a function to subtract from it.

The difficult bit is the collision detection, and that's not something I'm good with. I'd suggest you search it, or run through the tutorials which explain it very well.

I would have either tags or variables for each group, then on collision, check to see if the soldiers tag matches the building's tag and if it does then send a message to the building to add defenses and hide the soldier. Otherwise, send the soldier into attack mode.

A similar and easy way would be to make an enum for the different groups, give every object a script which decides that enum i.e. red team, blue team... then use GetComponent to grab that script, compare the enum of the soldier and the building and check to see if they match. This would be helpful if you were using tags for something else already.

ex.

//This script goes on the player.
function OnCollisionEnter(col : Collision) {
     if(col.gameObject.CompareTag(this.tag)) {
        col.gameObject.BroadCastMessage("AddReinforcments", SendMessageOptions.DontRequireReceiver);
        HideSoldier();
     }

     else {
        Attack(col.gameObject);
     }
}

function HideSoldier () {
     renderer.enabled = false;
}

function Attack (target : GameObject) {
     //attack the gameObject;
}
//Didn't test it.  Check for typos.