- Home /
How to Add an Individual Kill Count to Different Weapons
Hi there. I am in the process of making a 2D dungeon-crawler and I have 30 different weapons available in game. I want to add a kill counter to each weapon just as a little extra fun for the player to track their use of each weapon, but I can't quite seem to figure out the logic in going about that. I have an "Arsenal" panel that displays all the currently found/unlocked weapons (which that aspect works perfectly) and if you hold your mouse over the weapon, will display it's attributes, name, and some flavour text for the weapon. That stats panel is where I want to display the kill count for the weapon. In my "Unlocked Guns" script, I have added a Text object for the kill count which will be displayed on each weapons info panel, an int to hold the value, and am setting that kill count to PlayerPrefs so that it gets saved and stored for future plays. What I can't quite seem to get is how to actually add the kill count upon killing an enemy. I know I need to somehow associate the players current equipped weapon to that of the matching one in the Arsenal, and then also add the kill when the enemy dies to the kill count. This is pulling info and calling functions from three different scripts and I'm just not knowledgeable enough in C# to do this properly. Below is my "Unlocked Guns" script which holds the kill count and a portion of my "Enemy Controller" script which determines the death of the enemy.
Unlocked Guns:
public class UnlockedGuns : MonoBehaviour
{
public GameObject theGun, question;
public GameObject gunInfoPanel;
public int killCount;
public string kills;
public Text killText;
// Start is called before the first frame update
void Start()
{
if (PlayerPrefs.HasKey(theGun.name))
{
if (PlayerPrefs.GetInt(theGun.name) == 1)
{
theGun.SetActive(true);
question.SetActive(false);
}
else
{
theGun.SetActive(false);
question.SetActive(true);
}
}
if(Input.GetKeyDown(KeyCode.Escape))
{
HideInfoPanel();
}
}
// Update is called once per frame
void Update()
{
if (PlayerPrefs.HasKey(theGun.name))
{
if (PlayerPrefs.GetInt(theGun.name) == 1)
{
theGun.SetActive(true);
question.SetActive(false);
}
else
{
theGun.SetActive(false);
question.SetActive(true);
}
}
killText.text = "Kills: " + killCount.ToString();
}
public void ShowInfoPanel()
{
if(PlayerPrefs.GetInt(theGun.name) == 1)
{
gunInfoPanel.SetActive(true);
}
}
public void HideInfoPanel()
{
gunInfoPanel.SetActive(false);
}
public void AddKillCount(int killCounter)
{
killCount += killCounter;
PlayerPrefs.SetInt(kills, killCount);
}
}
One part I am also wondering is if I need to set a separate PlayerPrefs for each weapon or if I can use one value and since each weapon in the panel has it's own version of the script, it will automatically be separate?
Here's my enemy's death script where I think is where I need to add the kill count?:
public Gun killGun;
public UnlockedGuns uKillGun;
public int killCount = 1;
...
public void DamageEnemy(float damage)
{
currentHealth -= damage;
//Debug.Log("Damage: " + damage.ToString());
Instantiate(hitEffect, transform.position, transform.rotation);
healthSlider.value = currentHealth;
if (currentHealth <= 0)
{
anim.SetBool("isDead", true);
isDead = true;
int selectedSplatter = Random.Range(0, 4);
int rotation = Random.Range(0, 360);
int heartRotation = Random.Range(0, 360);
int heartDropRate = Random.Range(0, 5);
killGun = PlayerController.instance.availableGuns[PlayerController.instance.currentGun];
uKillGun.name = killGun.name;
uKillGun.AddKillCount(killCount);
Destroy(gameObject);
Instantiate(deathSplatters[selectedSplatter], transform.position, Quaternion.Euler(0f, 0f, rotation));
AudioManager.instance.PlaySFX(1);
if (heartDropRate >= 3)
{
Instantiate(deathSplatters[4], transform.position, Quaternion.Euler(0f, 0f, heartRotation));
}
//drop items
if (shouldDropItem)
{
float dropChance = Random.Range(0f, 100f);
if (dropChance <= itemDropPercent)
{
int randomItem = Random.Range(0, itemsToDrop.Length);
Instantiate(itemsToDrop[randomItem], transform.position, transform.rotation);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613031403im_/https://answers.unity.com/themes/thub/images/avi.jpg)