Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Zymurer · Sep 09, 2018 at 12:53 PM · serializationfieldstatic variableserializefield

serialize static fields

I am trying to make a simple video game and I need to use the "static" keyword for some reason

     static public int Level;

but when I do that it doesn't serialize a field for my variable. I tried to use this code but it didn't work (I looked at the scripting reference of unity after testing it. It also says SerializeField cannot serialize static fields. )

 [SerializeField()]
     static public int Level;

Is there any way to serialize a static field? Thanks for help from now.

Comment
Add comment · Show 2
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 Hellium · Sep 09, 2018 at 02:18 PM 0
Share

Why would you want to serialize a static field? Do you want Level to be accessible from other scripts? Then using static is not advised because it's not the purpose of the static modifier. Ins$$anonymous$$d, I advise you to keep Level as a simple integer and use another way to access the variable. Either by using a direct reference to your script (and take advantage of the drag & drop system of the Unity inspector, or if you don't have this possibility, to use the Find / FindObjectOfType functions), or by using the Singleton design pattern.

avatar image Zymurer Hellium · Dec 14, 2018 at 02:58 PM 0
Share

The problem is not about accessible. $$anonymous$$y game has multiple scenes and the player's level must be protected. When loading another scene.

1 Reply

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

Answer by PizzaPie · Sep 09, 2018 at 02:10 PM

You could create a helper field to store the value/ manipulate it in the editor and load it on Deserialization something like this.

 public static int myStaticInt;
 
     [SerializeField]
     private int intSerializationHelper;                  //don't use for anything else
 
     public void OnAfterDeserialize()
     {
         myStaticInt = intSerializationHelper;
     }
 
     public void OnBeforeSerialize()
     {
         //intSerializationHelper = myStaticInt;  // uncomment this if you want the field to reflect the value of the static field on playmode
     }

or probably better use another object (preferably a scriptable object to store the value), keep in mind that changes upon playmode will not remain after termination. This method is fairly unoptimized as it will perform the same action for every instance of said class. Don't forget to add the ISerializationCallbackReceiver interface on the class.

You could always hardcode the value.

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

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

Related Questions

Why does the prefabOverride not work on fields marked as SerializeField? 0 Answers

How to properly serialize fields and store them for use in play mode? 1 Answer

Deserialize a static class C# 1 Answer

usage of FieldInfo.SetValue with SerializeField 2 Answers

How to update serializedField showed on the inspector by script? 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