x


Score System help

Ok so I have a score system set up and i was wondering what do I put in the "Score" area so that the text displayed the scores that change

here is the script:

guiText.text = "Score:";
guiText.pixelOffset = Vector2 (10, 10);
more ▼

asked Mar 15 '12 at 03:39 AM

Galus07 gravatar image

Galus07
1 1 2 3

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, that script snippet isn't very useful, but that's ok.

Assuming you have a 'score' variable (a float or int or whatever that stores what the current score is), you can use

guiText.text = "Score: " + score.ToString();

to set the text. Put this in Update, otherwise it won't change as your score gets updated.

Otherwise, if you think it's too inefficient resetting the guiText.text every frame when your score doesn't update that often, you could inline it whenever you increment the score!

function AddScore(scoreToAdd : int)
{
    score += scoreToAdd;
    guiText.text = "Score: " + score.ToString();

}

Now, if you were using C# you could get even smarter, and set it up so that it's impossible to set the score without updating the GUIText!

private int _myScore;
private GUIText _myText;
public int score
{
    get{return _myScore;}
    set{
        if(_myText == null)
        {
            _myText = guiText;
        }
        _myScore = value;
        _myText.text = guiText.text = "Score: " + myScore.ToString();
    }
}

The fact that you can't do stuff like this in JavaScript is one reason I don't use that language...

more ▼

answered Mar 15 '12 at 03:44 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3320
x1943
x313
x273
x55

asked: Mar 15 '12 at 03:39 AM

Seen: 428 times

Last Updated: Mar 15 '12 at 03:44 AM