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.

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.