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 Connermarbut · Jun 10, 2020 at 06:16 PM · canvasnot workingdisplayscore systemontriggerenter2d

score display not showing

 void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "flower")
         {
             score += 1;
             Debug.Log(score);
         }
     }

I have a projectile that when hits the collider of the flower enemy type it adds a point to the overall score. The Debug.Log does show the score increasing yet the display text in the canvas doesnt up date. any idea why that might be?

Here's some screenshots of in game.

0pubash.png (59.9 kB)
iclumhl.png (92.7 kB)
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 Getsumi3 · Jun 10, 2020 at 06:48 PM

Hi

I don't see (on your screenshots) which script is making the job of updating the UI.

You'll need a script, like DataManager or GameManager, that will handle UI updating and data saving.

Here is a little example(not tested). Attach this script to an empty gameObject in your scene:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour
 {
     public int score = 0;
 
     public Text scoreText;
 
     public void AddScore(int _score)
     {
         //updating our variable
         score += _score;
         //updating the UI text
         scoreText.text = score.ToString();
     }
 }

And to your Flower.cs script add this changes:

 //Attach the empty object with GameManager script to this variable in inspector
 public GameManager GM;
 
 void OnTriggerEnter2D(Collider2D other)
      {
          if (other.gameObject.tag == "flower")
          {
              //adding score and updating UI
              GM.AddScore(1);
              Debug.Log(score);
          }
      }


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 Connermarbut · Jun 10, 2020 at 07:20 PM 0
Share

I did everything listed here, still seems to not update the UI. I called it Point $$anonymous$$anager, and added it to the players projectile. so when it hits the Flowers collider it would add to the point manager. Now weirder yet, after I test the game in Unity the Score amount doesn't reset? I've never seen this before..

alt text

capture.jpg (17.0 kB)
avatar image Connermarbut Connermarbut · Jun 10, 2020 at 09:42 PM 0
Share

I should mention that I'm using prefabs of the scoreText, and the projectiles. not sure how to do this without using prefabs. if that's the reason causing this, I'd appreciate to hear an alternative.

avatar image Getsumi3 Connermarbut · Jun 11, 2020 at 06:21 AM 0
Share

scoreText should be an actual object as a child in your canvas

Initialize your pointee$$anonymous$$anager as a static variable:

 public class Pointer$$anonymous$$anager: $$anonymous$$onoBehaviour
  {
         public static Pointer$$anonymous$$anager i;
 
         //everything else remains as before
  }

And on your projectile script you don't need anymore to attach a reference of Pointer$$anonymous$$anager so change it this way:

  void OnTriggerEnter2D(Collider2D other)
       {
           if (other.gameObject.tag == "flower")
           {
               //adding score and updating UI
               Pointer$$anonymous$$anager.i.AddScore(1);
               Debug.Log(score);
           }
       }

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

140 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 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

Canvas breaks on scene change 0 Answers

Simple score system breaking crates 1 Answer

score counter is not accurate 1 Answer

2D project - Sprite not visible in scene view 1 Answer

2D colliders not working with UI? 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