Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 edilla · Oct 23, 2014 at 12:20 AM · scoredeathpointsenemies

kill counter

I have a problem with the death of my enemies. when ever I kill an enemy I would like a kill count to go up by one point, but since I have a IEnumerator of 5 seconds before the gameobject is destoryed, the kill count goes up until the gameobject is destroyed. here's my code it's in C#

 IEnumerator enemyDead(){
             yield return new WaitForSeconds (5);
             Destroy (gameObject);
             
             
             
         }
     private bool alive(){
         bool alive = false;
         if (health > 0) {
             moveSpeed = 3;
             rotationSpeed = 5;
             alive = true;
             animation.CrossFade (runClip.name);
             Attack ();
         }
         return alive;
     }
 
     private bool dead(){
         bool dead = false;
         if (health <= 0) {
             moveSpeed = 0;
             rotationSpeed = 0;
             animation.CrossFade (dieClip.name);
             dead = true;
             StartCoroutine ("enemyDead");
             collider.enabled = false;
                         scoreManager.kills += 1;
                         scoreManager.killsCoins += 1;
                 }
             
 
         return dead;
     }
 }


here's my scoreManager

 public static int score;
     public static int kills;
     public static int killsCoins;
 
     GUIText text;
     public int currScore;
     public Font font;
     public Color color;
     public int winScreenWidth, winScreenHeight;
     private bool showWinScreen;
     public bool paused;
 
 
     void Awake (){
         //score = 0;
         if (PlayerPrefs.HasKey ("kills")) {
             kills = PlayerPrefs.GetInt("kills");        
         }
     }
     // Use this for initialization
     void Start () {
 
 
     }
     
     // Update is called once per frame
     void Update () {
         if (kills >= 20) {
             PlayerPrefs.SetInt ("kills", kills);
             showWinScreen = true;
             paused = true;
         }
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Habitablaba · Oct 23, 2014 at 01:07 AM

Move your 'dead' and 'alive' variables outside of the functions, and don't set them to false at the start of each.
Beyond that, perhaps you can collapse them into one. If the enemy is either alive or dead, you really only need to keep track if he is alive OR dead, not both (since alive is just not dead, and dead is just not alive).
Then, in your dead() method, just check to see

 if(health <= && !dead)

After that, let's assume you keep the dead variable instead of the alive one, set dead to false in the alive method.

Comment
Add comment · Show 3 · Share
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 edilla · Oct 24, 2014 at 12:10 AM 0
Share

cool that worked, but I now how a bug where my animation don't play, even though the enemy stop and is destroyed after the 5 seconds and the point goes up by 1. Here's the new code public AnimationClip dieClip; public AnimationClip idleClip; public AnimationClip runClip; private bool dead;

 void Update () {
 
     if (health > 0) {
         moveSpeed = 3;
         rotationSpeed = 5;
         animation.CrossFade (runClip.name);
         Attack ();
 }
 
 private bool isdead(){
 
       if (health <= 0 && !dead) {
         moveSpeed = 0;
         rotationSpeed = 0;
         animation.CrossFade (dieClip.name);
         dead = true;
         StartCoroutine ("enemyDead");
         collider.enabled = false;
         score$$anonymous$$anager.kills += 1;
         score$$anonymous$$anager.killsCoins += 1;
                 }
             
 
         return dead;
     }
 }
avatar image Habitablaba · Oct 24, 2014 at 12:13 AM 0
Share

This really should be its own question, especially since I have 0 experience with the Animation system. $$anonymous$$y initial advice would be to make sure you are setting the animation clips to the proper asset in the inspector.

avatar image edilla · Oct 24, 2014 at 12:30 AM 0
Share

Thanks, your the best

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why cant I subtract point from my score? 1 Answer

Collision and score 1 Answer

On screen scoring in multiplayer 1 Answer

Modified Roll a Ball Tutorial,Modified Roll a Ball tutorial question 1 Answer

[need help please] Score when AI Dies 0 Answers


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