Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 apie2546 · Nov 02, 2015 at 03:43 PM · score systemmain cameraupdating

Displaying Score

Always have trouble implementing a score counter in my games. I followed the 2d platformer tutorial. I took some portions of code from the roll a ball I did before to work on displaying score. I think I did this completely wrong and instead of waiting time, I wanted to ask for help. What I have done is assigned my score and win text to the Coin prefab, the ground spawns them randomly, and spawned coins come with the score and win text displayed with them. How do I change it to where the score text part is kept with the main camera, and when a coin is collided, it destroys like normal but updates a score counter. That is what has been throwing me off. I may not be googling my question right.

UPDATE:

I tried something different, on main camera I attached useri.cs using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class useri : MonoBehaviour {
 
     public Text countText;
     public Text WinText;
     public GameObject coin;
     public static int updatesc = 0;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void SetCountText ()
     {
         countText.text = "Score: " + updatesc.ToString ();
         
 
 }
 }

And on coin.cs using UnityEngine; using System.Collections;

 public class Coin : MonoBehaviour {
 
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     void OnTriggerEnter2D (Collider2D other)
     {
         if (other.gameObject.CompareTag ("Player")) {
 
             useri.updatesc ++;
             Debug.Log("Score!!!");
 
             Destroy(gameObject);
 
 
         }
     }
 
 
 
 }

score is now a static variable, but it will still not update the UI score, what am I missing here?

I am now getting Object reference not set to an instance of an object UnityEngine.UI.Graphic.OnRebuildRequested () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:454)

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
1
Best Answer

Answer by Garazbolg · Nov 02, 2015 at 04:10 PM

Alright, first you should use a log in OnTriggerEnter2D to know if your value in updatesc is right.

Second where do you use SetCountText() ?

And finally have you set the Text to an instance of Text in your inspector ?

Comment
Add comment · Show 6 · 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 apie2546 · Nov 02, 2015 at 04:22 PM 0
Share

I put debug log in the ontriggerenter and it showed, however the debug log under the class setcountrext does not display. So it is not getting back to the set count text class. And the text is assigned in inspector

avatar image Garazbolg apie2546 · Nov 02, 2015 at 04:47 PM 0
Share

Ok now try to call SetCountText () in your useri Update() like that :

 void Update(){
    SetCountText ();
 }

Does it work ?

avatar image apie2546 Garazbolg · Nov 02, 2015 at 05:04 PM 0
Share

The debug now shows SetCountText does display, but the debug from the OnTrigger does not show. I also have this error: NullReferenceException: Object reference not set to an instance of an object UnityEngine.UI.Graphic.OnRebuildRequested () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:454) UnityEngine.UI.Text.OnRebuildRequested () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Text.cs:523) UnityEngine.UI.GraphicRebuildTracker.OnRebuildRequested () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/GraphicRebuildTracker.cs:33) UnityEngine.CanvasRenderer.RequestRefresh ()

Show more comments
avatar image larku · Nov 06, 2015 at 05:31 AM 0
Share

Hey @apie2546 - can you elaborate a little more? I'm having the NullReferenceException at Graphic.cs:454 also but I'm not sure how you solved it (what was the issue with the prefab?)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why have newly added character animations made in blender stopped appearing in unity. 0 Answers

2 Simple Questions. The Questions Are Comments In The Code 1 Answer

Unknown issue with score system 1 Answer

I want my score to reset back to 0 but keep my highscore saved 3 Answers

How to make a simple scoring system with multiple triggers? 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