- Home /
The question is answered, right answer was accepted
In-game high score system
I am trying to make a high score system for each level.
I want to display high scores of level 1 in level 1 and high scores of level 2 in level 2.
Any help!?
This is my manager script which contains scores:
var TotalShips : int;
var ShipsKilled : int;
var enemeyShip : Transform;
var playerShip : Transform;
var playerLives : int = 2;
var player : GameObject;
var level : int = 1;
var score : int = 0;
var playerlive : int = 0;
var waitTime : float = 1.8;
var showlabel : boolean = true;
var gameover : boolean = false;
var mystyle : GUIStyle;
function Start () {
InvokeRepeating("Spawn", waitTime, waitTime);
}
function Update () {
}
function ShipCounter(){
if(TotalShips % 10 == 0){
level++;
}
return (level);
}
function Spawn(){
if(player == null && playerLives >= 1){
playerLives--;
Instantiate(playerShip);
player = GameObject.FindGameObjectWithTag("Player");
}
if(playerLives == 0){
gameover = true;
gameObject.FindGameObjectWithTag("Player").renderer.enabled = false;
}
}
function OnGUI(){
GUI.Label(Rect(0, 0, 100, 100), "" + score, mystyle);
}
Answer by vexe · Oct 12, 2013 at 08:20 AM
You could create a class for your levels, and have each level own its score value.
[C#]
public class Level
{
public int Score { get; set; }
// other stuff if you want...
}
And make a LevelManager
while you're at it:
public class LevelManager
{
public Level[] Levels { private set; get; }
public Level CurrentLevel { private set; get; }
private int index;
public LevelManager(int nLevels)
{
Levels = new Level[nLevels];
index = 0;
CurrentLevel = Levels[0];
}
public void NextLevel()
{
index = (index + 1) % Levels.Length;
CurrentLevel = Levels[index];
}
}
In your code:
private LevelManager levelManager;
public int nMaxLevels;
void Awake() // or Start
{
levelManager = new LevelManager(nMaxLevels);
}
Now whenever you need to increment the level, just do levelManager.NextLevel();
And, if you need to get the score of the current level: levelManager.CurrentLevel.Score;
The key thing to keep in mind, is to keep your classes have a single responsibility.
How about it?
EDIT:
[JS]
Level.js
var Score : int;
// other stuff if you want...
LevelManager.js
var Levels : Level[];
var CurrentLevel : Level;
private var index : int;
function LevelManager(nLevels : int)
{
Levels = new Level[nLevels];
index = 0;
CurrentLevel = Levels[0];
}
function NextLevel()
{
index = (index + 1) % Levels.Length;
CurrentLevel = Levels[index];
}
You don't need to attach Level
nor LevelManager
to anything.
In your code:
var levelManager : LevelManager;
var nMaxLevels : int;
function Awake() // or Start
{
levelManager = new LevelManager(nMaxLevels);
}
You'd access them the same way I described in the C# part ^.
@multigold99: Can you handle the C#->JS conversion, or do you want me to do it for you?
Btw I just updated it with this code But its not saving the score :'(
function OnGUI(){ GUI.Label(Rect(0, 0, 100, 100), "" + score, mystyle);
PlayerPrefs.SetInt('highscore', score);
PlayerPrefs.Save();
if(PlayerPrefs.Has$$anonymous$$ey('highscore')){
print(PlayerPrefs.GetInt('highscore'));
GUI.Label(Rect(0, 0, 100, 100), "" + PlayerPrefs.GetInt('highscore') , mystyle1);
}
}
O$$anonymous$$ I'm not a JS coder, and from what I know, in Unity when you create a JS script, JS automatically inherits from $$anonymous$$onoBehaviour. In your case, you just need a simple Level
and a Level$$anonymous$$anager
, they don't need to be $$anonymous$$onoBehaviours. But anyway, I'm gonna translate for you.
Updated, translated to JS. Let me know if you get any trouble.
Follow this Question
Related Questions
How to update high scores using PlayerPrefs? 1 Answer
Easy online highscore system 0 Answers
Accessing local system ( File Browser ) 2 Answers