Static Class not recognised at runtime

Hi guys,

I’m having trouble with a static class that I’ve just created for a game I’m building.

The class is public and static and lives in the same folder as all of the project’s other scripts. All of its variables and functions are also static.

Referencing the class (named PersistentData) from other classes in Visual Studio Community works just fine. The autocomplete works and there are no errors or warnings.

So far so good.

The problem comes when I’m actually running the game. Any attempts to access the methods or variables results in the following error:

I’m sure I’m missing something obvious, but I can’t think what it is. Any help would be much appreciated.

For completeness sake, the code is inserted below:

using UnityEngine;
using System.Collections;

public static class PersistentData {

    // PUBLIC ARRAYS
    public static int[] scores ;        // Holds all scores
    public static string[] scorers;     // Holds all scorers (in same order as scores)

    // PRIVATE VARIABLES
    private static int mostRecentScore; // The most recent score achieved in the game


    // AWAKE
    // Initialise our high-score table
    // Set default values for the scores and scorers
    static void Awake () {
        scores  = new int[]     { 100000, 50000, 25000, 10000, 5000 };  // Initialise high-scores
        scorers = new string[]  { "BAZ", "DAZ", "KAZ", "MAZ", "WAZ" };  // Initialise high-scorers
        mostRecentScore = 0;                                            // Initialise the most recent high-score
    }

    // SET SCORE
    // Set the score at a specific location in the array of scores
    // Used during updating of the high-score table
    public static void SetScore(int index, int score)
    {
        if (index >= 0 && index < scores.Length)
        {
            scores[index] = score;
        }
    }

    // SET SCORER
    // Set the scorer at a specific location in the array of scorers
    // Used during updating of the high-score table
    public static void SetScorer(int index, string scorer)
    {
        if(index>=0 && index < scorers.Length)
        {
            scorers[index] = scorer;
        }
    }

    // GET SCORE
    // Return the score at a given index in the array
    public static int? GetScore(int index)
    {
        if(index >= 0 && index < scores.Length)
        {
            return scores[index];
        }
        return null;
    }

    // GET SCORER
    // Return the scorer at a given index in the array
    public static string GetScorer(int index)
    {
        if (index >= 0 && index < scores.Length)
        {
            return scorers[index];
        }
        return null;
    }
}

And here’s the snippet of code that’s calling the static class’s functions and triggering the error:

    // START
    // Use this for initialization
    void Start()
    {
        Debug.Log("Score1: " + PersistentData.GetScore(0));
        Debug.Log("Score2: " + PersistentData.GetScorer(0));
    }

Awake is never called on a static class because its static…so your variables are never getting initialized therefore your arrays are null.

1 Like

I knew it’d be something stupid! :sweat_smile:

Thanks a lot, Polymorphik. That’s hugely helpful.