function to access multiple array.

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.

If you dont know how long your array needs to be, try a list instead.

Sorry if that wasnt he point of your post :wink:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
Generic list is the way to go if you need to change the size of something
Then to iterate through all values,
for(i=0; i<someList.count; i++){

}

So I got it to work. My problem was not naming the variable i. Once I did that I used it for both arrays/