Data from a simple singleton / static class return null

Here is my code for a singleton called Statistics :

public static Statistics instance;
    public int numberOfEnnemyKilled=0;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        } else
        {
            Destroy(this);
        }
    }

    public string GetTextEnnemyKilled()
    {
        return instance.numberOfEnnemyKilled.ToString();
    }

When i call it like that in another script :

text.text = Statistics.instance.GetTextEnnemyKilled();

I get the error NullReferenceException: Object reference not set to an instance of an object, saying that this function return null. I dont know what I’m doing wrong. Thanks!

It doesn’t look like you’re initializing instance.numberOfEnnemyKilled. Also you only posted some of your code so I’m not sure how to interpret what you have shown. You could try:

     public static Statistics instance;
 
     private void Awake()
     {
         if (instance == null)
         {
             instance = this;
             instance.numberOfEnnemyKilled = 0;
         } 
        else
         {
             Destroy(this);
         }
     }
 
     public string GetTextEnnemyKilled()
     {
         return instance.numberOfEnnemyKilled.ToString();
     }