• 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
Question by gimoj · Jun 09, 2014 at 03:25 PM · collisiongameobjecttriggerscoredetect

How to add the right amount of points when my player destroys a gameobject?

Can anybody help me with my scoring? I have it so that the player gets a point whenever it destroys an enemy but what happens is that sometimes it adds too many. It sometimes works as it should and adds 1 point to the score but it occasionally adds 3,4 or sometimes even 10. I don't know if it's also related but I tried making it so that an animation plays once when it hits the front check but the animation always plays twice.

How can I make it that it only adds 1 point whenever it destroys an enemy and/or only one animation plays?

Thanks, I think this is all basic but I've tried researching this all over the net but nothing works.

 void Awake()
 {
     frontCheck = transform.Find("frontCheck").transform;
     beak = transform.Find("beak").transform;
 }
 
 void Start () 
 {
     animator = GetComponent<Animator>();
 }

 void Update ()
 {
     walking = true;
     
 }

 void FixedUpdate ()
 {    
             GroundCheck ();

     if (grounded == false || dragged == true) 
     {
         float h = Input.GetAxis ("Mouse X");
         
         if (h > 0 && !facingRight)
             Flip ();
         else if (h < 0 && facingRight)
             Flip ();
     }

             if (grounded == true) 
                 {
                     if (walking == true) {
                             walker ();
                     }

                     Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position);
     
                     foreach (Collider2D c in frontHits) {

                             if (c.tag == "DirectionTrigger") {

                                     Flip ();
                                     break;
                             }

                             if (c.tag == "Enemy") {

                                     animator.SetTrigger ("dash");
                                     walking = false;

                                     break;
                             }
     
                     }

                 Collider2D[] beakhits = Physics2D.OverlapPointAll (beak.position);
                     
                 foreach (Collider2D b in beakhits) {
                         
                         if (b.tag == "Enemy") 
                         {
                             if (b.gameObject.GetComponent<Waspbot> ().BodyHit == true)
                             {
                                 b.gameObject.GetComponent<Waspbot> ().HitReceived (transform.position);
                                 CollateralDamage ();

                             }
                         }
                         
                     }

                 }

         if(HP <= 0 && !dead)
             {
                 Death ();
                 StartCoroutine(delayAnim());
             }
     }


 public void walker()
 {
     rigidbody2D.velocity = new Vector3 (transform.localScale.x * WalkSpeed, rigidbody2D.velocity.y, transform.localScale.y * WalkSpeed);
     
 }

 void GroundCheck()
 {
     Debug.Log ("grounded");
     Debug.DrawLine (sightStart.position, sightEnd.position, Color.blue);
     grounded = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("HighGround"));
 }
 
 public void Flip()
 {
     // Switch the way the player is labelled as facing.
     facingRight = !facingRight;

     // Multiply the x component of localScale by -1.
     Vector3 beakbotScale = transform.localScale;
     beakbotScale.x *= -1;
     transform.localScale = beakbotScale;
 }

 public void Hurt()
 {
     HP--;
     Debug.Log (HP);
 }
 
 void Death()
 {
     StartCoroutine(Explosion());
 }

 public IEnumerator delayAnim()
 {
     yield return new WaitForSeconds(0.01f);
     animator.SetTrigger ("explosion");
 }
 
 public IEnumerator Explosion()
 {
     if (HP <= 0) 
     {
         yield return new WaitForSeconds(0.6f);
         Destroy (gameObject);
         GameObject go = GameObject.FindGameObjectWithTag("Botton");
         Bottons sb = (Bottons)go.GetComponent(typeof(Bottons));
         sb.bbotcount -= 1;
     }
 }

 void OnMouseDown()
 {
     dragged = true;
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     
 }
 
 void OnMouseDrag()
 {
     Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
     
     Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) , offset;
     transform.position = curPosition;
     
 }

 void OnMouseUp()
 {
     dragged = false;
 }

 public void CollateralDamage ()
 {
     HP -= 1;
 }
 

 void OnTriggerEnter2D (Collider2D bbcol){
     
     // If it hits an enemy...
     if (bbcol.tag == "Enemy") {
         if (bbcol.gameObject.GetComponent<Waspbot> ().BodyHit == true)
         {
         bbcol.gameObject.GetComponent<Waspbot> ().RemoveHp ();
         HP -= 2;
         }
     }
 }
Comment

People who like this

0 Show 3
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 NoseKills · Jun 09, 2014 at 03:59 PM 0
Share

Where in this code is the score counting happening?

You could give the enemy scrips a bool that gets switched to true when you want to give the points from it and use the same boolean in an if condition to prevent doing it again.

avatar image gimoj · Jun 10, 2014 at 01:24 AM 0
Share

oh sorry I thought it was solely on my player, the scoring is in my enemy. Here's the code:

 void FixedUpdate ()
 {    
     
     if(gameObject.transform.position.x >= 0 && transform.localScale.x >=0)
     {
         // ... Flip the enemy and stop checking the other colliders.
         Flip ();
         
     }
     
     if (walking == true)
     {
         walker();
         
     }
     
     if (HP <= 0 && !dead) 
     {
         
         Death ();
         StartCoroutine(delayAnim());
         walking = false;

     }
     
     Debug.Log (HP);
 }
 
 public void Flip()
 {
     Vector3 waspbotScale = transform.localScale;
     waspbotScale.x *= -1;
     transform.localScale = waspbotScale;
 }
 
 public void walker()
 {
     rigidbody2D.velocity = new Vector2(transform.localScale.x * WalkSpeed, rigidbody2D.velocity.y);
 }

 void Death()
 {
     // Destroy the rocket.
     StartCoroutine(Explosion());
 }
 
 public IEnumerator delayAnim()
 {
     yield return new WaitForSeconds(0.01f);
     animator.SetTrigger ("explosion");
 }
 
 public IEnumerator Explosion()
 {

     if (HP <= 0) 
     {
         yield return new WaitForSeconds(0.6f);
         Destroy (gameObject);

         if (HitCore == false)
         {
             Scoring();
             
             // Create a vector that is just above the enemy.
             Vector3 scorePos;
             scorePos = transform.position;
             scorePos.y += 1.5f;
             
             // Instantiate the 100 points prefab at this point.
             Instantiate(hundredPointsUI, scorePos, Quaternion.identity);
         }
     }
 }
 
 void OnTriggerEnter2D (Collider2D col) 
 {
     // If it hits an enemy...
     //        if (col.tag == "swatter") {
     //            if (transform.position.x < 0f) {
     //                transform.Translate(Vector2.right * -15 * Time.deltaTime);
     //            } else if (transform.position.x > 0f) {
     //                transform.Translate(Vector2.right * 15 * Time.deltaTime);
     //            }
     //        }
 }
 
 public void bumpLeft()
 {
     transform.Translate (Vector2.right * -25 * Time.deltaTime);
 }
 
 public void bumpRight()
 {
     transform.Translate (Vector2.right * 25 * Time.deltaTime);
 }
 
 
 public void HitReceived(Vector3 playerPos){
     RemoveHp(); //remove those 2hp
     BumpHandler(playerPos);
 }
 
 public void RemoveHp(){
     HP -= 2;
 }
 
 private void BumpHandler(Vector3 player){
     if (player.x > transform.position.x){
         bumpLeft();
     }
     else{
         bumpRight();
     }
 }

 public void CoreHit()
 {
     GameObject go = GameObject.FindGameObjectWithTag ("Core");
     //Grab the script
     CoreScript sb = (CoreScript)go.GetComponent (typeof(CoreScript));
     //Call the function
     sb.HP -= 1;
     HitCore = true;
 }

 public void Scoring()
 {
     score.score += 1;
     scoreShadow.score += 1;
 }
avatar image gimoj · Jun 10, 2014 at 01:34 AM 0
Share

Sorry but I don't exactly know where it is.. I thought that maybe its related to some area of the script but here are the functions directly related to the scoring

void FixedUpdate ()

{

 if(gameObject.transform.position.x >= 0 && transform.localScale.x >=0)

 {

    Flip ();

 }

 if (walking == true)
 {
    walker();

 }

 if (HP <= 0 && !dead) 
 {

    Death ();
    StartCoroutine(delayAnim());
    walking = false;

 }

 Debug.Log (HP);

}

void Death()

{

 StartCoroutine(Explosion());


}

public IEnumerator delayAnim()

{

 yield return new WaitForSeconds(0.01f);
 animator.SetTrigger ("explosion");


}

public IEnumerator Explosion()

{

 if (HP <= 0) 
 {
    yield return new WaitForSeconds(0.6f);
    Destroy (gameObject);

    if (HitCore == false)

    {
      Scoring();

      // Create a vector that is just above the enemy.
      Vector3 scorePos;
      scorePos = transform.position;
      scorePos.y += 1.5f;

      // Instantiate the 100 points prefab at this point.
      Instantiate(hundredPointsUI, scorePos, Quaternion.identity);

    }
 }

}

public void HitReceived(Vector3 playerPos)

{

 RemoveHp(); //remove those 2hp
 BumpHandler(playerPos);


}

public void RemoveHp()

{

 HP -= 2;


}

public void Scoring()

{

 score.score += 1;
 scoreShadow.score += 1;


}

Sorry I can't isolate it more but this is my first game and I'm having trouble getting it done.

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

23 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

Related Questions

If gameObject's center passes through a trigger? 1 Answer

game issues with infinte collisions 2 Answers

Whats wrong with my score script 1 Answer

Collide with projection 1 Answer

Mission Objectives 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