Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
6
Question by MasterChameleonGames · Mar 27, 2019 at 07:56 PM · warningserializefield

[SerializeField] Field is never assigned to, and will always have its default value 0

The warning "Field is never assigned to, and will always have its default value 0" appears whenever using [SerializeField] on a variable.

Is there any other solution to this other than disabling the error message and using public instead?

Yes, I know this has been addressed before, but are there any new solutions?

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 WarmedxMints · Mar 27, 2019 at 10:00 PM 2
Share

Just assign a value in code and it will shut up. Either = 0, = null or whatever is required.

avatar image MasterChameleonGames WarmedxMints · Mar 28, 2019 at 12:38 AM 0
Share

Well yeah, should've included that in the post, forgot to

4 Replies

· Add your reply
  • Sort: 
avatar image
20

Answer by Bunny83 · Mar 28, 2019 at 12:17 AM

WarmedxMints is right. From a pure OOP point of view nobody is able to actually change the value of the variable since it's private. Most serialization systems actually bypass the protection level of variables in order to read / write the values through reflection. This has nothing to do with adding the SerializeField attribute. That attribute is just a custom attribute that comes with Unity. The compiler has no idea what this is good for. Attributes actually don't do anything as they are just metadata. It's always some code of a subsystem which actually reads and interprets attributes.


The compiler warning is just a warning, not an error. You can not "tell" the compiler that this field is initialized through reflection. You can tell it to ignore the warning (which is not a great solution). The best fix is to simply initialize the variable yourself. It can be initialized with 0 / null.

Comment
Add comment · Show 2 · 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 MasterChameleonGames · Mar 28, 2019 at 12:39 AM 0
Share

Well yeah, should've included that in the post, forgot to

avatar image darthdeus · Aug 24, 2020 at 06:58 PM 0
Share

You can not "tell" the compiler that this field is initialized through reflection

Is this actually true? $$anonymous$$odern compilers, especially Roslyn, have a bunch of flexibility in what they can do. I guess since Unity uses $$anonymous$$ono it might be a bit more involved, but with a Roslyn analyzer I'd imagine you really just write a custom rule for the [SerializeField] property, no?

avatar image
5

Answer by andrew-lukasik · Jul 17, 2020 at 10:53 PM

Guys, listen to Bunny83. Don't obfuscate nor complicate this. Just initialize fields to something, anything, null, zero or default(T); case closed.


 [SerializeField] private string text = null;

look maa no `#pragma warning disable`


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

Answer by CDMcGwire · Apr 25, 2020 at 11:45 PM

For anyone using Rider or other editors/linters with warnings about redundant initializations, I'll post the stategy I've been using.

As per this SO question about general C# warning suppression, you can wrap your private serialized fields in #pragma preprocessor statements to disable the compilers check for CS0649.

 #pragma warning disable 0649
     [SerializeField] private string group;
     [SerializeField] private string kind;
     [SerializeField] private int order;
     [SerializeField] private int precedence;
 #pragma warning restore 0649

On the downside, it adds more verbosity to the code, but it also makes sure to limit the scope to just your reflection populated fields. It also plays nice with most linters and makes it clear to other (probably new to Unity) devs that there's some non-standard stuff going on here.

And if you don't mind including the whole file in the scope, you can just leave out the restore directive. Though, in my case I naturally group my private serialized fields together anyways, so it's clean enough and I only have to do it once per file.

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

Answer by aydin_khp · Jul 17, 2020 at 09:15 PM

The approach I use for this situation which works better for me than turning off the warnings is to change private to protected. This way the other classes are still unable to change that field - which is the main reason I decided to use [SerializeField] private instead of public fields in the first place. The only downside for this solution is that the derived classes will have access to this field but I really doubt this would be an issue in a game.

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

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

SerializeField does not work when a value set using code 0 Answers

How to add the score in a quiz game with text input field and a button. 0 Answers

Warning messages are turning into Internal Compiler Errors...? 6 Answers

Warning, null Texture passed to GUI.DrawTexture 0 Answers

Box Collider 2D -failed verification- warning even when the size is reasonable 2 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