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);
}

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 ^.