Ok so I'm attempting to create a high score system and am using an array of a class within an object, however the error in the title appears which tells me that for some reason my objects are full of null variables, here are my classes:
class HighScores
{
public HighScore[] MyScores = new HighScore[5];
protected bool failedRead = false;
private void SortHighScores(string NewName, int NewScore)
{
HighScore[] TmpScores = new HighScore[6];
for (int i = 0; i < 5; i++)
{
TmpScores[i].Name = MyScores[i].Name; // Error Occurs here for `MyScores[i].Name` however in output box both MyScores and TmpScores show null
TmpScores[i].Score = MyScores[i].Score;
}
TmpScores[5].Name = NewName;
TmpScores[5].Score = NewScore;
Array.Sort(TmpScores, delegate(HighScore x, HighScore y) { return x.Score.CompareTo(y.Score); });
Array.Reverse(TmpScores);
MyScores = new HighScore[5];
for (int i = 0; i < 5; i++)
{
MyScores[i].Name = TmpScores[i].Name;
MyScores[i].Score = TmpScores[i].Score;
}
}
}
class HighScore
{
public string Name = "hello world";
public int Score = 0;
}
And here is where I initialise an object of HighScores
:
HighScores GameScores;
protected override void Initialize()
{
base.Initialize();
GameScores = new Highscores();
}
Surely the objects shouldn't be null if I made them as new
?
[EDIT]
OK so I've made a new function within the class HighScores to initialise the MyScores objects as new HighScore objects:
public void InitScores()
{
for (int i = 0; i < 5; i++)
{
MyScores[i] = new HighScore();
}
}
Which is called within protected override void Initialize()
(I'm sure you're all familiar with it, it's standard in XNA). I also have added the same code in the class function: GameScores.SortHighScores
to initialise TmpScores
.
Still the same error, still the same place where it occurs, any ideas?