- Home /
Simple score question
Here's what I want to do; achieve this score (say 100) and load next level. And here's my script:
#pragma strict
var score : int =0;
function Start ()
{
GetComponent(TextMesh).text = PlayerPrefs.GetInt("score", 0).ToString();
}
function Update ()
{
if(score == 100){
Boo();
}
}
function Boo(){
Application.LoadLevel("Level2");
}
I'm sure it's something dead simple but i am really new to this.
You have never assigned the score variable or I'm missing something?
Answer by aldonaletto · Apr 01, 2012 at 04:47 PM
I don't know exactly what's going wrong in your case, but you should save the score value read in the variable score, or it will be only appear in the 3D text:
function Start ()
{
score = PlayerPrefs.GetInt("score", 0); // save the value in score
GetComponent(TextMesh).text = score.ToString(); // then copy it to the text mesh
}
I get errors on score=Play... line.
Assets/Scripts/score.js(9,9): BCE0049: Expression 'score' cannot be assigned to.
and
Assets/Scripts/score.js(10,41): BCE0020: An instance of type 'UnityEngine.Object' is required to access non static member 'ToString'.
Have you changed something in the code you've posted? The variable score is correctly declared in the script posted, thus these errors should not occur (they seem to indicate that score has not been declared yet).
I don't get the errors anymore - but I don't get any other result either. Here's the updated script.
var score : int =0;
function Start ()
{
score = PlayerPrefs.GetInt("score", 0); // save the value in score
GetComponent(Text$$anonymous$$esh).text = score.ToString();
}
function Update ()
{
if(score == 100){
Debug.Log("Score is 100");
Boo();
}
}
function Boo(){
Application.LoadLevel("Level2");
}
But which results do you expect? The score value is loaded at Start, and nothing in this script is changing it. Unless other scripts increment the variable score (or if its value in PlayerPrefs is 100) the Level2 will never be loaded. You should also move the assignment to Text$$anonymous$$esh.text to the Update function, so any score change would appear in the text:
function Update ()
{
getComponent(Text$$anonymous$$esh).text = score;
if(score >= 100){ // handles score >= 100 case
Debug.Log("Score is >= 100");
Boo();
}
}
Your answer
Follow this Question
Related Questions
Scoreboard will not update 1 Answer
Don't destroy script on load 2 Answers
Money System Not Working Please Help!!!! (Code fully commented!) 3 Answers
When should I save data 2 Answers