- Home /
What's the deal with the ..cctor() error?
This is the error I'm getting:
NullReferenceException
GameUtils..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for GameUtils
GameUtils is a static class containing game state information and such. In my main menu scene, the StartGame button sets the CurrentGameState property in GameUtils before loading the level, but this error comes up, so the level doesn't load. However, if I start in the level and quit to the menu, I don't get this error and the level loads fine.
The MainMenu script uses several public fields from GameUtils, so I don't see how GameUtils can be null.
Edit: Code included below
// this is in MainMenu OnGUI()
if (GUI.Button(new Rect(GameUtils.BUTTON_WIDTH / 2,
Screen.height - (4 * GameUtils.BUTTON_HEIGHT),
GameUtils.BUTTON_WIDTH, GameUtils.BUTTON_HEIGHT), "Match Play"))
{
GameUtils.CurrentPlayMode = PlayMode.Match;
Application.LoadLevel("Playfield");
}
// this is the property being accessed in GameUtils
public static PlayMode CurrentPlayMode { get; set; }
where PlayMode is an enum. I'm using an auto-implemented property; I've tried using a private field and a regular property that gets/sets it, but it made no difference.
Sounds like something is throwing an exception in the constructor code to me... Can you post it?
There is no constructor.
1) Scripts in Unity can't have constructors as far as I know
2) GameUtils is a static class, so definitely no constructor there
lets see the code from the line referenced in the error.
Are you calling a function or public variable inside GameUtils?
$$anonymous$$aybe your attempting to get a value from a function or variable inside GameUtils and that value itself is null not the function.
$$anonymous$$aybe its default value is null and your attempting to access it.
Scripts in Unity can have constructors if they are not $$anonymous$$onoBehaviours
And variables assigned in the script are effectively part of the classes constructor:
var x : int = 5;
That should always work, something more complicated is throwing the exception.
Answer by swatmaster69 · Mar 24, 2013 at 09:38 PM
I found the answer here. I was calling GameObject.Find()
in my static class, but the object I was trying to find didn't exist in the Menu scene, so Find()
returned null
and crashed the initialization. This is the line that was causing the crash:
public static readonly float COURT_WIDTH = 10 * GameObject.Find("Court").transform.lossyScale.x;
It was tricky to figure this out because when I started from the Playfield scene, everything worked fine since the object (the court) I was searching for was in the scene, so Find()
did find it. Yeesh...
you are correct . Same error found in my project. After removing GameObject.Find(), it work properly
Answer by jonc113 · Nov 07, 2013 at 03:29 AM
I'm late to the party, but adding this in case someone else get the ..cctor error
Just to re-iterate, the problem occurs when you define AND initialize a variable (in my case, also public static) that uses a reference that doesn't exist yet - or hasn't been initialized yet.
Almost always, unity gives you the line number of the error, but NOT in this case - insidious!
Your answer
Follow this Question
Related Questions
Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer
NullReferenceException Error how to solve? 2 Answers
Static Dictionary not initializing properly 1 Answer
Data from a simple singleton / static class return null 1 Answer
should class members ever be static? 1 Answer