Adding kills to global kill count?

Hello I dont really have a lot of experience with List so I need some help
When i hit enemy, He register the name who hit him the last and i have a basic GlobalKillcount script with list on it
List
List

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Annoucer : MonoBehaviour {

    public List<AnnoucerData> Data = new List<AnnoucerData>();

   
}


using UnityEngine;
using System.Collections;

public class AnnoucerData : MonoBehaviour {

    public string Name;
    public int Kills;
}

what i need to write in to the death function to check if the list contains the player name and if it doesnt create one section in list and add one kill or if it already exists in the list just add one kill to it.

Health script

using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour {

    public int HealthPoints;
    public string LastHitBy;
    // Update is called once per frame
    void ApplyDamage (int Damage) {
        HealthPoints -= Damage;

        if (HealthPoints <= 0) {
            Death();
        }
    }
    void Death() {

        Destroy (gameObject);
    }
}

Sounds like a dictionary might be better?

1 Like

Oh sorry, I was on hurry :smile:
Let me sort this out,
What I want to do Is to register Player kills to the list.
So what I’ve done by now is only register who killed the enemy, but what I need to know Is how can I send this information to my List, how to check If that player have killed anyone in the past, If he did, just add one kill in to hes data, If not, create a new section for him and so on…

Bored is still corrent - a dictionary would be better.

You’d want something like:

Dictionary<string, int> killCountDict;

void RegisterKill(string playerName) {
    if(killCountDict.HasKey(playerName)) {
        killCountDict[playerName]++;
    }
    else {
        killCountDict.Add(playerName, 1);
    }
}
1 Like

Dictionary the collection type, not dictionary “your post was misspelled”… :sunglasses:

1 Like

Thanks for the answer! While waiting for It I figured that out by myself xD
Is it good to?

public void Check(string name) {
        Debug.Log (name);
        for (int i = 0; i < Data.Count; i++) {
            if (Data [i].Name.Contains (name)) {
                Debug.Log (i);
                Data[i].Kills += 1;
                return;
            }
            else {
                Data.Add (name, 1);
            }
        }
    }

The only error I get is in the line
Data.Add (name, 1);

For some reason It says “No overload for method Add' takes 2’ arguments”

I’m guessing it’s because you’re using a List instead of a Dictionary<string, int>?

If you really want to use the AnnouncerData class (it’s just a wrapper around a data pair, so not really very usefull), you’ll have to create a new instance of that class, set the name and kill count, and add it to the list.