Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by swatmaster69 · Nov 19, 2012 at 12:01 AM · nullreferenceexceptionstatic

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Nov 19, 2012 at 12:03 AM 0
Share

Sounds like something is throwing an exception in the constructor code to me... Can you post it?

avatar image swatmaster69 · Nov 19, 2012 at 03:21 AM 0
Share

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

avatar image sparkzbarca · Nov 19, 2012 at 05:51 AM 0
Share

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.

avatar image whydoidoit · Nov 19, 2012 at 08:04 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LijuDeveloper · Nov 18, 2013 at 10:27 AM 0
Share

you are correct . Same error found in my project. After removing GameObject.Find(), it work properly

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges