- Home /
GUI Text not displaying/updating correctly (C#)
Hi, I am new to Unity and I'm running into an issue with GUI Text. In my game, the player throws a ball and tries to get them into baskets. For each basket the player makes, their score increases. There is also a point multiplier based on the number of times throws you go without missing a shot. Here is my code (I'm using C#):
public class ballEnter : MonoBehaviour {
private int score = 0; //total score
private int mult = 0; //streak multiplier
public GUIText scoreText; //GUIText to display score to player
void Start () {}
void OnTriggerEnter(Collider ball) //ball enters a bucket
{
Destroy(ball.gameObject); //ball is destroyed
if (mult == 0) //updates score when there is no streak multiplier
score++; //adds one to the score
else //updates score with streak multiplier
{
mult++; //increases multiplier by one
score += mult; //adds to score factoring in the added points from the multiplier
}
}
void OnTouchFloor(Collider ball)
{
Destroy(ball.gameObject); //destroys ball
mult = 0; //resets multiplier to 0
}
void Update() //displays score to gameview screen
{
scoreText.text = "Score: " + score;
}
}
What's working:
The ball is destroyed when it enters a basket
The ball is destroyed if it touches the level (basically if the shot is missed)
The score starts at zero and Score: 0 is displayed in the inspector under the text property of my GUI Text object
What's not working:
I have a GUI Text object in my hierarchy named scoreText, it's a child of the canvas in my game and I have this script attached to it, but when I start the game there is no GUI Text displayed on the game screen, it's only displayed in the inspector
When the ball enters a basket, the score is not updated at all
I've tried watching tutorials and reading similar questions on here, but nothing has helped so far; any answers are greatly appreciated. Thanks.
Okay, so I've been working on this for the past week and now I got it to display the score to the screen. $$anonymous$$y new issue is that it does not get rid of previous numbers, so ins$$anonymous$$d of "Score: 0" changing to "Score: 1", it writes the one over the zero and it becomes hard to read. And ins$$anonymous$$d of updating the score only when a ball enters a bucket, it also adds to the score if the balls miss the basket and hit the floor/walls. Also, the multiplier is still not working.
$$anonymous$$y updated code: public class ballEnter : $$anonymous$$onoBehaviour {
public int score = 0; //total score
private int mult = 0; //streak multiplier
void OnTriggerEnter(Collider ball) //ball enters a bucket
{
Destroy(ball.gameObject); //ball is destroyed
if (mult == 0) //updates score when there is no streak multiplier
score++; //adds one to the score
else //updates score with streak multiplier
{
mult++; //increases multiplier by one
score += mult; //adds to score factoring in the added points from the multiplier
}
}
void OnTouchFloor(Collider ball) //ball misses bucket and hits floor/wall
{
Destroy(ball.gameObject); //destroys ball
mult = 0; //resets multiplier to 0
}
void OnGUI() //displays score to gameview screen
{
GUI.Label(new Rect(10, 10, 100, 20), "Score:" + score);
}
}
Again, any help is appreciated.
Answer by nikcio · Oct 21, 2016 at 08:08 PM
Hi if you use a canvas text instead of a guitext you can do this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ballEnter : MonoBehaviour {
private int score = 0; //total score
private int mult = 0; //streak multiplier
public Text scoreText; //GUIText to display score to player
void OnTriggerEnter(Collider ball) //ball enters a bucket
{
Destroy(ball.gameObject); //ball is destroyed
if (mult == 0) //updates score when there is no streak multiplier
score++; //adds one to the score
else //updates score with streak multiplier
{
mult++; //increases multiplier by one
score += mult; //adds to score factoring in the added points from the multiplier
}
}
void OnTouchFloor(Collider ball)
{
Destroy(ball.gameObject); //destroys ball
mult = 0; //resets multiplier to 0
}
void Update() //displays score to gameview screen
{
scoreText.text = "Score: " + score;
}
You simply just have to make a canvas text and reference it, and then change the two things in the code:
using UnityEngine.UI;
and
public Text scoreText;
Hope it helps.
Thank you very much @nikcio! I changed the code and now it works for the most part; however, now when I press play I get an error: "NullReferenceException: Object reference not set to an instance of an object". I created a canvas text object in the hierarchy, and it has a spot in the Inspector named Score Text. When I click in the box the only option I have is the text object itself. I'm not quite sure what to do from here.
Never$$anonymous$$d, I figured it out. I had the code attached to the ball ins$$anonymous$$d of the level.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Paint text of GUIText ? 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Detecting collisions 3 Answers