- Home /
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
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
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.
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
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;
}
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());
}
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));
}
}
You would need to change "playerscore" (in my code) to score, in your case.
Wow, $$anonymous$$ay 06 '13 at 11:39 A$$anonymous$$. What the heck was I looking at.
Your answer
Follow this Question
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