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 Ryuuguu · Feb 01, 2014 at 03:09 AM · nullreferenceexceptionplaymoderecompile

How to setup code so it can recompile without null exception while running

How do oyu setup code so it does not throw null exceptions when recompiled while running. I know this could be done in Unity 2.x and I suspect it still can be done. I know it had to do with setting up variables but don't remember what had to be not used. Static variables? unserialised variables?

note: This is a duplicate question but previous question was only answered by someone who did not know about unity and just said it can't be done. Please only answer "it can't be done" if you understand how it used to be done and know that that will not work anymore.

Cheers, Grant

Comment
Add comment · Show 1
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 Benproductions1 · Feb 01, 2014 at 09:55 AM 1
Share

your variables just need to be serializable.

1 Reply

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

Answer by gfoot · Feb 01, 2014 at 11:21 AM

When your code is recompiled, all class instances are destroyed, but things Unity knows how to serialize are serialized first and recreated afterwards. So your MonoBehaviours will get reattached to your GameObjects, and members that Unity can serialize will have their values preserved. Anything else will be null.

In some cases making more things serializable will help, but it's not always practical - it depends on how complex your classes are. Some things are easier to recreate from scratch after the recompile, based on other members that were serialized. There's no explicit notification, but I believe OnEnable gets called at this point, so you can put code in there to detect the problem (e.g. test something for null) and perform any fix-up you want to do, reconstructing whichever objects were lost.

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 vexe · Feb 01, 2014 at 11:32 AM 2
Share

Right - when I first started I didn't even know what serializing mean. So it's good to mention that as well. @OP: (basically) serializing something means storing that thing to disk (there's binary serialization, xml, soap, etc) - Deserializing something, is the opposite - it's loading an object into your app from disk.

I used to suffer from this too - but basically you just have to keep an eye out of what are the types that Unity serializes, and what it doesn't. Here, have a look.

If you have a variable of type X, assu$$anonymous$$g X is something that unity can serialize, if you set X's privacy to public, it will be serialized by default otherwise (private/protected) you should mock the field with [SerializeField].

Note that, if you want to have a private variable (hidden from inspector) to be serialized, mocking it with SerializeField will make it visible so you have to mock it with [HideInInspector]

Also, helps to read this.

Good luck :)

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

21 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

Related Questions

Unity Re-compiles on Play 1 Answer

Playmode script recompilation - static vars lost? 0 Answers

NullReference with trigger C# ? 1 Answer

Null Reference Excepting :Object reference not set to an instance of object while changing text of 3D text 2 Answers

NullReference on textfield 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