Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 aman_jha · May 18, 2014 at 09:51 PM · c#updatescoreguitextinvokerepeating

GUIText not updating score....

So I got this guitext that holds my score, and depending on the distance that the player has traveled before he (inevitably) dies, the score updates. But for some reason, when i press the lovable play button, The score GUI is there, but it doesn't update. Not only that, its weird because what is displayed is the final score from the last time the game is played. For example, if you play one game, and die at at 254 meters, when you press "Try Again" (this reloads the scene through Application.LoadLevel), it starts again with the score GUI saying "Score: 254". From then on it doesn't update.

My code to hold the score is below:

 using UnityEngine;
 using System.Collections;
 
 public class ScoreScript : MonoBehaviour {
 
     //Scores
     private double startPos = 0;
     private double currentPos;
     private int score;
 
     //Score Stuffs
     public GUIText CurrentScore;
 
     // Use this for initialization
     void Start () {
 
         score = 0;
 
         Instantiate(CurrentScore, new Vector3 (0.35f, 1f, 0f), Quaternion.identity);
         
     }
     
     // Update is called once per frame
     void Update () {
         currentPos = (int)(transform.position.x);
             
         score = currentPos;
 
         print (""+score);
         CurrentScore.text = "Score: " + score.ToString();
     }
 }
 
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
3
Best Answer

Answer by Kiwasi · May 19, 2014 at 12:28 AM

You're doing some really weird stuff here. But I can solve your problem for you.

Put simply you instantiate your CurrentScore once in the start function. Instantiate creates a new copy of your object. Then in the update function you update CurrentScore.

Here is what is happening

  1. Start() runs. Unity creates a CurrentScore(clone) object. "Score: 0"

  2. Update() runs. CurrentScore is updated. CurrentScore(clone) is not

  3. User reloads the level. CurentScore(clone) is deleted on level load. (Something outside of your code is also preventing the GameObject containing ScoreScript from being destroyed.)

  4. Start() runs. A new CurrentScore(clone) is created from the present value of CurrentScore. "Score: 254"

  5. Update() runs. CurrentScore is update. CurrentScore(clone) is not.

There are multiple ways to solve this problem. First you could assign CurrentScore to a GUIText you have created in the scene viewer. Remove line 19 completely. I have always had trouble working with GUIText directly and would nor recommend this method

Or you could use unity's built in OnGUI function. Code as follows:

 using UnityEngine;
 using System.Collections;
  
 public class ScoreScript : MonoBehaviour {
 
     //Scores
     // By default unity uses floats instead of doubles
     // The other variable aren't actually needed in this version of the script
     private int score;
 
     //Score Stuffs
     private string scoreText;
  
     // Use this for initialization
     void Start () {
        //Updating scoreText here as well as in update removes the carryover when loading levels
        score = 0;
        scoreText = "Score: " + score.ToString(); 
     }
  
     // Update is called once per frame
     void Update () {
        score = (int)(transform.position.x);
        scoreText = "Score: " + score.ToString();
     }
 
     void OnGUI (){
         GUI.Label (new Rect(20,20,100,100), scoreText);
         //Alternatively use
         GUILayout.Label(scoreText);
     }
 }
Comment
Add comment · Show 1 · 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 aman_jha · Jun 24, 2014 at 02:26 AM 0
Share

Sorry I didn't accept your answer for so long. I've avoided using the default unity GUI systems because they don't look good, but I recently discovered how to use GUI Skins, so they're fine with me now ;)

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

multiply Score on Collision not working? check my code plz 2 Answers

Distribute terrain in zones 3 Answers

Show score when enemy is destroyed C# 1 Answer

Using InvokeRepeating in the Update method? 2 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