- Home /
Question by
lvl · Jan 23, 2014 at 12:01 AM ·
c#classstatic script
System.NullReferenceException
For simplicity's sake, let's say I have 3 C# scripts: Main, Globals, Data. All are classes within the same namespace, and one (Main) is attached to a gameobject.
public class Main:
...
byte[] tmp = new byte[16];
Globals.data.SetKey(tmp);
...
public static class Globals:
...
public static Data data;
...
public class Data:
public void SetKey(byte[] key)
{
//Do Stuff
}
As you can see, I'm trying to call the SetKey function from the Main script.
This compiles without issue, and shows no errors in the editor. It plays, and throws no errors. However the script does not complete. Debugging shows a System.NullReferenceException on Globals.Data.SetKey(tmp);
Compiled outside of Unity, the script runs as expected. Any help would be appreciated!
Comment
Answer by rutter · Jan 23, 2014 at 12:04 AM
Do you ever instantiate a Data
for Globals.data
?
The simplest solution I picture is:
public static Data data = new Data();