- Home /
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)
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 ?
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
Ok now try to call SetCountText () in your useri Update() like that :
void Update(){
SetCountText ();
}
Does it work ?
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 ()
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
Follow this Question
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