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 androids · May 06, 2013 at 11:39 AM · guitextscorecounter

GUI Text Score counter

In my game, you play with time. the longer you stay in the game, the more points you get. So if the game starts, your score is 0. It counts from 0 to 7 in 1 second. Now I don't know what I do wrong, but I can't fix this problem. Someone who can help me out?

EDIT

This is what I got right now, but I don't know how to add a GUI Text that counts from 0 to ... (no end).

 var score: int = 0;
 var addscore = 7;
  
 function Update() {
        addscore += Time.deltaTime;
 }

Please help :D

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 ahaykal · May 06, 2013 at 12:00 PM 0
Share

could you show us what you have written so far so that we could see where the problem is?

If you are using update try using FixedUpdate,

or multiply the time by Time.deltaTime

3 Replies

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

Answer by roojerry · May 06, 2013 at 05:07 PM

 function OnGUI(){
     GUI.Box(Rect(Screen.width/2,Screen.height/2,100,25), score.toString());
 }

will make a GUI Box for you with your score in it. However, it doesn't look like you are ever adding values to your score variable so your score counter may need to be tweaked.

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 androids · May 06, 2013 at 06:04 PM 0
Share

All right, but it's not counting. It stays on "0" but it needs to go from 0 to ...

This is what I got right now

 var score: int = 0;
 var addscore = 7;
 
 function Update() {
        addscore += Time.deltaTime;
 }
 
 function OnGUI(){
     GUI.Box(Rect(Screen.width*0.5,Screen.height*0.5,100,25), score.ToString());
 }

Please help me. Because nothing happend

avatar image roojerry · May 06, 2013 at 06:16 PM 0
Share

As I said in my answer, you are never changing the score variable. That is the variable you must add to if you want to see the changes printed. Something like this should do what you want:

  InvokeRepeating("Add",0, 1);
 
 function Add() {
      score += 7;
 }
avatar image
1

Answer by ExTheSea · May 06, 2013 at 06:18 PM

I used InvokeRepeating when i did a timer in my game like this:

 function Start(){
 InvokeRepeating("EachSecond",1.0,1.0);
 }
 
 var score: int = 0;
 var addscore = 7;
 
 function EachSecond()
 {
 score=score+addscore;
 }
 
 function OnGUI(){
 GUI.Box(Rect(Screen.width*0.5,Screen.height*0.5,100,25), score.ToString());
 }
Comment
Add comment · 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
0

Answer by Dblfstr · Feb 05, 2014 at 02:07 AM

This will draw a GUI label 20 units left, 20 units down, 100W * 30H With the word Score then the actual players score.

 GUI.Label (new Rect (20, 20, 100, 30), "Score " + (int)(playerScore));

And I am not sure what this is supposed to do.

 var score: int = 0;
 var addscore = 7;
  
 function Update() {
        addscore += Time.deltaTime;
 }

But I would say

 var score: int = 0;
 var addscore = 7;
  
 function Update() {
        score += addscore*Time.deltaTime;
 }



EDIT: Here is a code I have actually used. The score increases constantly over time. But Time.deltaTime is small, so I multipy by 100. Then I have OnGUI to show the score as I play.

 using UnityEngine;
 using System.Collections;
 
 public class ScoreScript : MonoBehaviour {
 
     float playerScore = 0;
 
 
     // Update is called once per frame
     void Update () 
     {
         playerScore += Time.deltaTime;
     }

     //Method to increase score though other scripts:
     //Like collectibles, etc.
     public void IncreaseScore(int amount)
     {
         playerScore += amount;
     }
 
     void OnDisable()
     {
         //Look to store somewhere else. Like a packet to persist with dont destroy on load.
         PlayerPrefs.SetInt ("Score", (int)(playerScore*100));
     }
 
     void OnGUI()
     {
         GUI.Label (new Rect (10, 10, 100, 30), "Score " + (int)(playerScore * 100));
     }
 }
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 Dblfstr · Feb 05, 2014 at 02:09 AM 0
Share

You would need to change "playerscore" (in my code) to score, in your case.

avatar image Dblfstr · Feb 05, 2014 at 02:16 AM 0
Share

Wow, $$anonymous$$ay 06 '13 at 11:39 A$$anonymous$$. What the heck was I looking at.

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

17 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

Related Questions

coin collecting with onscreen score 1 Answer

3D ^ Custom GUIText/Label or Making a Score System without using GUI? 0 Answers

Score/point counter system 1 Answer

Problem with GuiFont - Not appearing 0 Answers

Animating Transitions for GUI Results 0 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