C# CS1519 and CS0178 array of a class in another script

In one script I have a class that holds a string and an int for displaying and asocating with a highscore

using UnityEngine;
using System.Collections;

public class Highscore : MonoBehaviour
{ 
	string name; 
	int points; 
}

In another script I want to access this information and keep it in an array, so I can add new highscores (modify values and shift scores around for sorting and remove lowest score)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HighScoreGUI : MonoBehaviour 
{
	Highscore highScore = new Highscore[10]; 
	
	highScore[0].name = "bob"; //errors are here
}

When I try to set a value for the first highScore I get error:

CS0178: Invalid rank specifier: expected ,' or ]’

and also error:

CS1519: Unexpected symbol `.’ in class, struct, or interface member declaration

Thanks for helping!

2 Answers

2
  1. It seems Highscore should be a separate class, not a component. Don’t derive it from MonoBehaviour and consider changing it to struct.

  2. Both name and points should be marked public to be able to access them from outside of the class.

  3. Array declaration should be:

    Highscore highScore

not

Highscore highScore

that’s the cause of CS0178

  1. Before accessing element of an array, it should be initialized:

    void Start()
    {
    highScore[0] = new Highscore(); // I assume Highscore class does not derive from MonoBehaviour
    highScore[0].name = “bob”; // this must be inside of a method - you can’t do this outside (that’s the cause of CS1519)
    }

Thank you, I couldn't have received a more complete answer than this

You declared the variable highscore as of type Highscore, this is your error.

you need :

Highscore[] highScore = new Highscore[10];

ArkaneX was quicker than me :)

Thanks, I tried to give thumbs up to both of you but I lack the rep, I'll come back later when I can