- Home /
Countdown timer reaches, if score higher than 1 load next level??
Hi and thankyou for looking at my problem. I am very new to unity, followed a few tutorials but i am stumped with this problem. I am making a simple head the ball game, and i want the player to be able to move on to the next level if his score is higher than 1. i cannot work out how to add both of those into the script below. I have tinkered a little bit and now when i run the game it just goes straight to the menu scene, i think this is because of the timer command. If anyone could point out what i am doing wrong i would be very grateful.
Many thanks.
var score3DText : TextMesh;
private var score : int=0;
var timer3DText : TextMesh;
var timer : float = 10.0;
function Update()
{
timer -= Time.deltaTime;
timer3DText.text = "Time: " +timer.ToString();
if(timer<=0)
{
timer = 0;
if(score>=1);
{
Application.LoadLevel("level2");
}
}
else
{
Application.LoadLevel("menu");
}
}
function Awake()
{
InvokeRepeating("UpdateScore", 0.05, 0.05);
}
function UpdateScore()
{
score += 0;
score3DText.text = "Score: " + score.ToString();
}
function AddScoreForBonusArea()
{
score += 1;
}
function GameOver()
{
if(score > PlayerPrefs.GetInt("highScore"))
{
PlayerPrefs.SetInt("highScore", score);
}
Application.LoadLevel("menu");
}
Answer by Xitech_ · May 13, 2014 at 01:27 PM
I think because you save the score into
function GameOver()
{
if(score > PlayerPrefs.GetInt("highScore"))
{
PlayerPrefs.SetInt("highScore", score);
}
Application.LoadLevel("menu");
}
PlayerPrefs is probably bigger than the variable score. So it goes to the menu A reminder. PlayerPrefs saves your variable. Even when you exit and relaunch the game.
EDIT: You can remove the gameover function. Then it will work again. EDIT2: Read about PlayerPrefs here: https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
Hi, thankyou for replying. Should this code work for when the timer reaches 0, and if the player has scored more than 1 goal, then level2 will be loaded? At the moment as soon as the game starts it just reverts back to the menu screen.
I have taken out the GameOver function as you previously suggested.
function Update()
{
timer -= Time.deltaTime;
timer3DText.text = "Time: " +timer.ToString();
if(timer<=0)
{
timer = 0;
if(score>=1);
{
Application.LoadLevel("level2");
}
}
else
{
Application.LoadLevel("menu");
}
}
Yes. But you should remove timer = 0; Now you do: If times <0.. Timer = 0.. It makes no sense. Does it work correctly now?
yes i have it working perfect now, thankyou for all your help!
Answer by Kiwasi · May 15, 2014 at 07:35 PM
Problem with the Update() over code is the extra curly bracket on line 18. Delete that and it will function perfectly.
Your answer