-1

I've got two classes, one called Simulation, another called Star shown below:

public class Simulation
{
    public static Simulation simulationInstance;

    public void test()
    {
        Debug.Log("test");
    }
}

public class Star
{
    public void test2()
    {
        Sim.simulationInstance.test();
    }
}

I'm trying to call the test() method in Simulation from the test2() method in Star.

But this returns an error:

NullReferenceException: Object reference not set to an instance of an object

Pointing to this line:

Sim.simulationInstance.test();

What am I doing incorrectly, and how do I fix this?

SidS
  • 185
  • 9
  • 1
    Duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Feb 22 '20 at 19:22

1 Answers1

2

It is because you never instantiate simulationInstance. You should do that like this:

public class Simulation
{
    public static Simulation simulationInstance = new Simulation();

    ...
Bart Friederichs
  • 30,888
  • 13
  • 85
  • 169