I have two arrays stored in a “Enemy” object: who hit him and how much damage that was dealt.
When the enemy object dies, I would like to go through the “who hit him” and give them points/ exp. equal to the “damage that was dealt”
Here is how I have it figured so far, but this isn’t for the entire array as I can’t know before hand how long the array will be.
var whoHitMe : GameObject [] ;
var howMuchDamage : float [] ;
function OnCollisionEnter ( other : Collision) {
//If the object touched isn't a spell, then the script will not activate.
if (other.gameObject.tag != "Spell")
return;
else if (other.gameObject.tag == "Spell"){
Debug.Log("Spell hit Enemy!");
GetComponent(HealthandMana).playerHealth -= other.gameObject.GetComponent(spellController).spellPower;
addToWhoHitMe (other.gameObject.GetComponent(spellController).spellOwner);
addToHowMuchDamage (other.gameObject.GetComponent(spellController).spellPower);
if (GetComponent(HealthandMana).playerHealth <= 0) {
if (GetComponent(HealthandMana).playerLevel + 4 <= whoHitMe[0].GetComponent(HealthandMana).playerLevel){
whoHitMe[0].GetComponent(HealthandMana).expLevel += 5;
Debug.Log("Gave 5 exp cause mob is too low!");
}
else {
Debug.Log("Gave " + (howMuchDamage[0] * 0.1) * (GetComponent(HealthandMana).playerLevel / whoHitMe[0].GetComponent(HealthandMana).playerLevel) + "experiance.");
whoHitMe[0].GetComponent(HealthandMana).expLevel += (howMuchDamage[0] * 0.1) * (GetComponent(HealthandMana).playerLevel / whoHitMe[0].GetComponent(HealthandMana).playerLevel);
}
}
Destroy(other.gameObject);
}
}
function addToWhoHitMe (obj : GameObject) {
whoHitMe += [obj];
}
function addToHowMuchDamage (num : float) {
howMuchDamage += [num];
}
As an example if an enemy is hit by multiple people, each will get part of the exp when the enemy dies. I hope I am being clear as I can sometimes not be as effective in communication as I would like.