• 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 /
  • Help Room /
avatar image
Question by chainsoftAU · Nov 12, 2015 at 04:29 PM · c#javascriptuiscore

My UI Score Is Not Working?

Okay, when it comes to programming, I am a noob. I usually use Javascript even though I don't know jack squat about it, but I've sort of learnt how it works with the if statements and variables and all, but when I tried C#, which I was terrified of using, it went exactly how I thought it would.

So, I'm trying to get the UI working for my game, a cartoony-bloody border with score and high score text in the top left corner. I have a functioning GUI, but it looks ugly and I know I can do better. So I'm trying to set up the UI to display the score. The C# tutorial I watched had some other score system going on, there wall a ball hitting a rope and the score system was all in the one script, but I could make out some of the script and thought I could set the score to display a PlayerPref int whatever it's called.

But as you can see in the images below (the maximum is 2, so I put them all in 1) it didn't work. I've included the other scripts involving score.

So maybe someone could help me out? Also, the game involves driving a car around and running over pedestrians, just letting you know so you understand the gameplay I'm going for. And as an extra, could someone tell me how to get a countdown timer in the UI as well? For 60 seconds, BTW.

Thank you! :D

TLDR Version: Used C# for the first time, score UI doesn't work.

alt text

ui-help-unity.jpg (317.8 kB)
Comment

People who like this

0 Show 2
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 ZefanS · Nov 13, 2015 at 02:16 AM 0
Share

Could you post your code as code blocks in your question? It would be easier to help if we could copy-and-paste instead of typing it all from scratch from the image.

avatar image chainsoftAU ZefanS · Nov 16, 2015 at 12:48 AM 0
Share

Soz, I'll be sure to do that from now on :)

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by KrabyGame · Nov 14, 2015 at 05:25 PM

First, assign a gameobject with a Text component on it to the scoreText variable (drag it from hierarchy to the inspector, in Unity), it should work

I think you should avoid using PlayerPrefs in Update / FixedUpdate/OnGUI or everything that is executed every frame

In scoreUI, you get the "yourScore" value from playerpref every Update (every frame) You should use the currentScore variable in scoreKeeper.js Same in OnGUI , replace the PlayerPrefs.GetInt(...) by highScore.


For the countdown you can add a float variable somewhere, let say scoreUI.cs (btw, why do you use both cs and js ?), set it to 60 in the Start function and in update, add

 countDown -= Time.deltaTime;

deltaTime is the time in second between the last frame and the current one. Then make a timeText variable which will contains the text that display the time , and update it in Update It should looks like this

 public class scoreUI : MonoBehaviour
 {
 
     public Text scoreText, timeText;
     public float timeLeft;
 
     void Start()
     {
         timeLeft = 0;
         if (scoreText || timeText)
             Debug.LogError("Assign Text gameobjects in the inspector");
     }
 
     void Update()
     {
         scoreText.text = "Score: " + scoreKeeper.currentScore;
         timeText.text = "Time: " + timeLeft;
         timeLeft -= Time.deltaTime;
     }
 }

Comment

People who like this

0 Show 4 · 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 chainsoftAU · Nov 16, 2015 at 12:41 AM 0
Share

Whoops! Heh, forgot something. I didn't set the score text thing in the component. alt text

My game was actually playable, but the score still wasn't displaying. And I couldn't make out half of what you said, so I tried doing whatever I could read from that, but it didn't work. THE STRUGGLE! Ugh. I guess I should just put the GUI in the corner and just publish this frickin thing already.

scoremistake.png (5.7 kB)
avatar image chainsoftAU · Nov 16, 2015 at 12:47 AM 0
Share

Also, I tried the script above and this is what happened: alt text

morefukingproblems.png (95.0 kB)
avatar image chainsoftAU · Nov 18, 2015 at 10:56 PM 0
Share

NM I've got a functioning GUI now. I'll probably fix it later.

avatar image chainsoftAU · Nov 19, 2015 at 02:53 AM 0
Share

alt text

How's this?

gui.png (299.5 kB)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How add(create) a new gameobject on scene on mouse button click. 2 Answers

Translate this javascript for me? 2 Answers

How can I use a float Variabel in an other Sript? 1 Answer

how to show score using panel 0 Answers

How do i turn on/off background music and keep the changes when reload the scene 1 Answer


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