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
0
Question by J1M · Aug 13, 2014 at 04:18 AM · serializationloadingorder

How to change script load order? (Serialization problem)

I have a series of classes that use public static instances of themselves to mimic the enum functionality offered by Java. As you might imagine, there were some tricks to serializing and deserializing those static instances, but I have that sorted out now. My test objects save and load from disk correctly.

Unfortunately, when I apply the [Serializable] attribute to my character class, I run into several variations of the following error on startup:

 NullReferenceException: Object reference not set to an instance of an object
 Namespace.DurationType..cctor ()
 Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Namespace.DurationType

It appears that putting the [Serializable] attribute on this particular class causes something on it to be executed before anything else in my codebase. This is very confusing to me, since the null error that is logged suggests that the static constructor on my enum classes has not been called properly.

Is there a way to force a certain script to be handled first by Unity? If so, I would expect I could force my static constructor to run before this weirdness occurs with the character class.

Note: none of the classes I'm serializing are derived from MonoBehaviour.

Update: The problem appears to be caused by Unity creating an instance of one of my classes before allowing its type initializer to be run. This is caused by having a script in the editor reference a class that uses my character object.

If I use a startup script to attach the character object dynamically, the problem goes away.

This is pretty clearly a Unity bug. Still looking for a better workaround than "script everything instead of using the editor".

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by RedDevil · Aug 13, 2014 at 05:57 AM

Yes there is...in fact you can make all scripts go in what order you want.I was experiencing a similar issue. GO to EDIT->Project Settings->Script Execution Order and there you can add or remove what script goes first from top to bottom. :)

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 J1M · Aug 13, 2014 at 11:43 PM 0
Share

I put my initialization script at -10000 in the script execution order settings. I also adding a logging message at the top of its Awake() and OnEnable() methods.

Whatever execution is occurring on my Character class is happening before the script marked to execute first. If I remove the [Serializable] attribute from Character the problem goes away and the game loads properly. (But I can't save it...)

avatar image
0

Answer by CypherJH · Apr 01, 2017 at 06:59 AM

Little late, but... Another solution would be to use Awake, instead of Start, since this is handle before any Start function, according to the monobehaviour lifecycle diagram.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to assign a class ID at runtime? 0 Answers

How to control serialized variables over the inspector pane? 1 Answer

Script not executed when upgrading to Unity 4.3 0 Answers

Serialized class to google games services cloud save 0 Answers


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