• 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 MichaelTheGreat · Nov 05, 2014 at 04:34 AM · kills

Help on kill counter script!

I put a kill counter script inside my enemy health script, so (theoretically) every time the object (enemy) gets destroyed, the counter goes up by one. using UnityEngine; using System.Collections;

 public class Health : MonoBehaviour {
     public static int kills;
     public int hp = 3;
         public bool isEnemy = true;
     // Use this for initialization
     void Awake()
     {
                 kills = 0;
         }
     void OnGUI()
     {
         GUI.Label(new Rect(0, 0, 100, 25), "Kill Count: " + kills);
     }
     void OnTriggerEnter2D (Collider2D collider) {
         ShotScript shot = collider.gameObject.GetComponent<ShotScript>();
 
         if (shot != null) {
                         if (shot.isEnemyShot != isEnemy) {
                                 hp -= shot.damage;
 
                                 Destroy (shot.gameObject);
 
                                 if (hp <= 0) {
                                         Destroy (gameObject);
                     Health.kills++;
                             
                                 }
                         }
                 }    
 
     }
 }
 

Here is what happens currently: Every time the health script executes (its a wave game, so it executes every second or so) *The Gui pops up *The enemy count goes back down to zero

I need it to where the GUI stays on the moment the game starts instead of It popping up when I execute the health script and the kill count does not reset. I know the code is tied into the health script so it's no surprise that this happens, but any help would be great.

Comment
Add comment · Show 4
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
avatar image Ricewind1 · Nov 05, 2014 at 05:12 AM 0
Share

Why not put the killcount and other shared data on a separate script/gameobject such as the player or a data gameobject. This way you can manage things much more easily, and if needed you can serialize this class too.

Just reference this script/object in the enemy and add a kill every time just before you destroy it.

avatar image incorrect · Nov 05, 2014 at 07:08 PM 1
Share

@yashpal, you are totally wrong. Try this:

 using UnityEngine;
 using System.Collections;
 
 public class TestScript : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
         Destroy(gameObject);
         Debug.Log ("It works!");
     }
 }

Btw, you don't know what static variables are, so you think they are destroyed too when an instance of a class is destroyed. Try reading more about them.

avatar image MichaelTheGreat · Nov 05, 2014 at 09:56 PM 0
Share

It works perfectly now! Thank you!

I just wondered but how would I be able to add, lets say, If you kill this enemy, you get 2 points, if you kill THIS one you get 1 etc? I would assume you would use the FindGameObjectsWithTag function, but I do not know to word it to where it would "work"

avatar image yashpal · Nov 06, 2014 at 04:48 AM 0
Share

@incorrect, Thank you. I just deleted my comment. you are correct.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

A [Photon] PvP Script. 0 Answers

My script adds +1 to variable only one time 1 Answer

deathmatch point system 1 Answer

Killer + assistance id for shooter game 1 Answer

Score increases based on number of collisions at time of kill, not on single kill.,Score increases based on collisions at time of death, not based on single kill. 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