Null error trying to create my own number system. How the heck do I fix?

So I have been getting null results and null reference errors from this script

Null reference: object reference not set to an instance of an object

I have tried trouble shooting this, but basically I am trying to create a system that allows me to display a huge number to players, while being able to make a code that can run it that goes larger than doubles, but is probably a bit messier… point is, how the heck do I put my number system script and class to use?

public class Number_System : MonoBehaviour
{
    public static Number_System instance;
    public double exponent;
    public double mantissa;

    // instantiaziation, and enabling use
    private void Awake()
    {
        instance = this;
    }
    // the maths of the number systems, currently supports addition, subrtaction, multiplication and division, but not any higher powers, and division
    public void Number_subtraction(Number_System x, Number_System y)
    {
        if ((y.exponent- x.exponent) > - 4)
        {
            x.mantissa -= y.mantissa *System.Math.Pow(10, y.exponent - x.exponent);
        }
        
    }
    public void Number_addition(Number_System x, Number_System y)
    {
        if ((y.exponent - x.exponent) > -4)
        {
            x.mantissa += y.mantissa * System.Math.Pow(10, y.exponent - x.exponent);
        }    
    }
    public void Number_multiplication(Number_System x, Number_System y)
    {
        x.exponent += y.exponent;
        x.mantissa *= y.mantissa;
    }
    public void Number_division(Number_System x, Number_System y)
    {
        x.exponent -= y.exponent;
        x.mantissa /= y.mantissa;
    }
    public void Update()
    {
        if (mantissa >= 10)
        {
            mantissa= mantissa/10;
            exponent = +1;
        }
        if (mantissa < 1 && mantissa > 0 )
        {
            mantissa = mantissa*10;
            exponent = -1;
        }
        if (mantissa < 0 && exponent >= 0)
        {
            mantissa = mantissa / -1;
        }
    }
}

public class Data_system 
{
    public Number_System color;
    public Data_system()
    {
        color.mantissa = 0;
        color.exponent = 0;
    }
}

public class Base_game : MonoBehaviour
{
    public Data_system data;
    // this code is setting up the instance to be analyzed by other scripts
    public static Base_game instance;
    private void Awake()
    {
        instance = this;
    }
}

public class Text_System : MonoBehaviour
{
    public Data_system data;
    public string color;
    void Update()
    {
        data = Base_game.instance.data;
        Debug.Log(data);
    }
}

This code looks unnecessarily complicated. Also - Number_System shouldn’t be a class let alone MonoBehaviour (!) because a struct is for values and numbers are values in a very fundamental sense.

What comes to the error itself - it happens in Data_systems constructor as Number_System color field is null:

public class Data_system 
{
    public Number_System color;// this is null
    public Data_system()
    {
        color.mantissa = 0;// color is null but your are trying to access it thus error
        color.exponent = 0;// same
    }
}

If you want my advice:

  • delete Number_System

  • delete Data_system

  • delete Base_game

  • delete Text_System

  • complicated code means deadly low productivity - keep it simple, solve real problems; not imaginary ones

  • use my BFN struct and modify it to suits your needs

  • write this code again, but in a way that solves something, not just to produces complexity (i.e.: problems for the next day)