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 LadyPushkin · Oct 14, 2016 at 08:34 PM · c#triggersguitextscoring

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.

Comment
Add comment · Show 1
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 LadyPushkin · Oct 21, 2016 at 06:58 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · 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 LadyPushkin · Oct 24, 2016 at 07:04 PM 0
Share

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.

avatar image LadyPushkin LadyPushkin · Nov 01, 2016 at 06:34 PM 0
Share

Never$$anonymous$$d, I figured it out. I had the code attached to the ball ins$$anonymous$$d of the level.

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

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


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