• 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 Hefaz · Jan 20 at 09:14 PM · c#uiscoretagscollision2d

Score text is not updating on collision

Hello,

I have multiple prefabs in a scene with two tags called Square and Player. they have a boxCollider2d attached to them.

these prefabs has a health point and i decrease it when the collision occurs. then I check if a health point for any object is 0, then score +1 and so on...

Now, the problem is that is updating only, when a gameObject tagged as Player health point is reached to zero. What is the problem with this script?

 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System;
 
 public class Smash : MonoBehaviour
 {
     public int hitPoints;
     public Text score;
     public Text highScore;
     public int mc = 0;
 
     
     // Use this for initialization
     void Start()
     {
         highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
     }
 
 
     // Update is called once per frame
     void Update()
     {
     }
 
     public void OnCollisionEnter2D(Collision2D collision)
     {
         
         if (collision.gameObject.tag == "Square" || collision.gameObject.tag == "Player")
         {
             hitPoints--;
             var getText = gameObject.GetComponentInChildren<TextMesh>();
             getText.text = hitPoints.ToString();
             if (hitPoints == 0)
             {
                mc++;
                 StartCoroutine(BrokenSqaures());
             }
             if (mc > PlayerPrefs.GetInt("HighScore", 0))
             {
                 PlayerPrefs.SetInt("HighScore", mc);
                 highScore.text = mc.ToString();
             }
         }
     }
 
     private IEnumerator BrokenSqaures()
     {
         
         score.text = mc.ToString();
         yield return new WaitForSeconds(particle.main.startLifetime.constantMax);
         Destroy(gameObject);
     }
 
 }
 
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

Answer by Llama_w_2Ls · Jan 20 at 10:12 PM

The issue is here:

              if (hitPoints == 0)
              {
                 mc++;
                  StartCoroutine(BrokenSqaures());
              }

You only start the coroutine called BrokenSquares, which sets your score text to mc.ToString() when your health is zero. I recommend you just put this line score.text = mc.ToString(); outside the coroutine and into the collision function, so it is called whenever you change the text. @Hefaz

Comment
Add comment · Show 5 · 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 Hefaz · Jan 20 at 10:24 PM 0
Share

but i use some other things as well inside the coroutine and I need the text only to be updated whenever the health is zero. @Llama_w_2Ls

avatar image Hefaz · Jan 20 at 10:26 PM 0
Share

for example here public void OnCollisionEnter2D(Collision2D collision) {

         if (collision.collider.gameObject.tag == "Square" || collision.collider.gameObject.tag == "Player")
         {
             mc++;
             score.text = mc.ToString();
         }
     }

the text is updating only if it hits the object Tagged as Player and not updating when two objects with the Square tag are collided.

avatar image Llama_w_2Ls Hefaz · Jan 21 at 08:51 AM 0
Share

Have you checked as to whether the square objects ever collide? Try putting a Debug.Log in there to see whether the collision function is called correctly. $$anonymous$$aybe you're missing a rigidbody or collider.


Also, you should use if (collision.collider.gameObject.CompareTag("Square") ...) It's a bit more efficient, according to Unity $$anonymous$$essages.

avatar image Hefaz · Jan 21 at 10:43 AM 0
Share

Yeah, collision is working fine. I will make the question clearer.

How do you update a score text, when a health point of an object has been reached to 0, if there are multiple collissions. each collision decrease one health point.

something like this.

alt text

sssssss.png (12.4 kB)
avatar image Llama_w_2Ls Hefaz · Jan 21 at 11:05 AM 0
Share

There should be a script on each square that handles collisions. It should look something like this:

 public class Square : $$anonymous$$onoBehaviour
 {
     public int health;
     public Text Score;
 
     void OnCollisionEnter2D(Collider2D col)
     {
 
          if (col.collider.gameObject.CompareTag("Square"))
         {
              Square square = col.collider.gameObject.GetComponent<Square>();
              square.health -= 1;
              this.health -= 1;
          }
      }
 
      void Update()
      {
            if (this.health == 0)
            {
                   Score.text = (int.Parse(Score.text) + 1).ToString();
             }
       }
 }

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

114 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

Related Questions

Activate Void update if with UI Button 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Save HIght Score c# (double) 1 Answer

Which type of Variable do I need to use to change the Value of a text UI object ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges