Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PilgrimFlowers · Mar 27, 2021 at 05:32 PM · unity 2dhow-tohow todungeon

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);
                     }
                 }
             }
     
         }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to stop rooms from spawning on top of each other with list,How to make sure rooms don't spawn in the same place 0 Answers

I made a game in which a ball moves randomly and if the player presses a ball he gains some points, I want if he does not press the ball he loses some points 1 Answer

How to make a moveable lever? (2D) 0 Answers

How do I make custom clothes move with the character rig in Unity2D? 1 Answer

How to Walljump? (3D) Need Help! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges