I have two gameobjects, lets call them 0 and 1. 0 and 1 attack each other in an attack, counterattack pattern, once per second. I use 3D arrays to keep track of how many healthy units they have in each army, since there are many types and tiers of units. The one that is important here is numberOfHealthUnits. I am using a MapUnit class to hold this 3d array. The gameObject unit is instantiated and there are two of them on the map. Both contain the MapUnit class.
The problem, is when I have them attack, both in the attack (which 0 → 1 means 1 takes unit damage), then counterattack (1 → 0 means 0 takes damage), the units that are killed are both subtracted from 1 3D array. In the Debug below, you can see how it goes from 1000 → 816 → 678 when it should be (1000 → 816) && (1000 → 862).
My first thought is that the 3D array must be treated as global for some reason, but it is not static. I also can’t find much unity docs on 3D arrays anyways. If this is true, I could make it a 4D array with an index for each unit, or instead manually variable out each class, which would be a big pain.
Any thoughts? Thanks.
Code:
public class MapUnit : MonoBehaviour
public int[,,] numberOfHealthUnits = new int[4,5,1] {
//Number of Units
{{1000},{0},{0},{0},{0}}, //Infantry, {T1 - T5}
{{0},{0},{0},{0},{0}}, //Artillery, {T1 - T5}
{{0},{0},{0},{0},{0}}, //Armored Vehicles, {T1 - T5}
{{0},{0},{0},{0},{0}} //Transport, {T1 - T5}
};
private void AttackTakeDamage(float avgEnemyAtt, float totalDMGTook) {
Debug.Log("Target: " + this.gameObject.name);
var combatEnvironment = 0; //0 = field, 1 = city
var totalHealthyUnits = 0;
for(int a = 0; a < numberOfHealthUnits.GetLength(0); a++) {
for(int b = 0; b < numberOfHealthUnits.GetLength(1); b++) {
totalHealthyUnits += numberOfHealthUnits[a, b, 0];
}
}
var ratioUnitsToTotal = 0f;
var ratioOfDefenseToEnemyAttack = 0f;
var unitsSurvived = false;
for(int a = 0; a < numberOfHealthUnits.GetLength(0); a++) {
for(int b = 0; b < numberOfHealthUnits.GetLength(1); b++) {
if(numberOfHealthUnits[a, b, 0] > 0) {
ratioUnitsToTotal = numberOfHealthUnits[a, b, 0] / totalHealthyUnits;
ratioOfDefenseToEnemyAttack = avgEnemyAtt / AM.GetUnitDefense(a, b, 1);
Debug.Log("Hit: DMG Took: " + totalDMGTook + " R Units: " + ratioUnitsToTotal + " R Def: " + ratioOfDefenseToEnemyAttack + " UHealth: " + AM.GetUnitHealth(a, b, 1));
var totalHitUnits = Mathf.Ceil((totalDMGTook * ratioUnitsToTotal * ratioOfDefenseToEnemyAttack) / AM.GetUnitHealth(a, b, 1));
Debug.Log("Hit: Type: " + a + " Tier: " + b + " Num: " + totalHitUnits);
Debug.Log("B4: " + numberOfHealthUnits[a, b, 0]);
numberOfHealthUnits[a, b, 0] = numberOfHealthUnits[a, b, 0] - (int)totalHitUnits;
Debug.Log("After: " + numberOfHealthUnits[a, b, 0]);
if(combatEnvironment == 0) {
numberOfSlightlyWoundedUnits[a, b, 0] = numberOfSlightlyWoundedUnits[a, b, 0] + (int)totalHitUnits;
}
if(numberOfHealthUnits[a, b, 0] > 0)
unitsSurvived = true;
}
}
}
if(!unitsSurvived) {
DefeatUnit();
}
}
Debug:
Attack!
Friendly Units:
Healthy:
Type: 0Tier: 0Num: 1000
Target: Unit Preset(Clone)0
Hit: DMG Took: 23000 R Units: 1 R Def: 0.9583333 UHealth: 120
Hit: Type: 0 Tier: 0 Num: 184
B4: 1000
After: 816
Target: Unit Preset(Clone)1
Hit: DMG Took: 17250 R Units: 1 R Def: 0.9583333 UHealth: 120
Hit: Type: 0 Tier: 0 Num: 138
B4: 816
After: 678
As you can see from the debug, 0 and 1 both call AttackTakeDamage() when damage was done to them. The call is local to that gameObject, as by the printed name, however the damage seems to be both to the same 3D array (numberOfHealthUnits). I want to reference the specific array for that specific gameObject.