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 /
  • Help Room /
avatar image
15
Question by microp · Apr 24, 2011 at 10:41 PM · warning

warning CS0649: Field is never assigned to, and will always have its default value `null'

Hello! I recently switched from UnityScript to C#, and now that everything compiles fine, I get countless CS0449 warnings. Of course the fields are never assigned to, the user is meant to set the value from the editor. How can I cleanly get rid of that warning?

Many thanks!

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 Justin Warner · Apr 24, 2011 at 11:10 PM 0
Share

Try just initializing them to blank strings "" and what not?

avatar image Ipsquiggle · Jun 14, 2011 at 05:21 PM 0
Share

The answers solve the problem, but don't really explain why dozens of scripts compile without error, and then one or two give this complaint for all their public fields.

avatar image Bitpocketer · May 01, 2015 at 05:38 PM 0
Share

Well, I'm having the same issue even though the variables are public and assigned properly in inspector. I can't seem to grab what's going on.

avatar image MrCranky Bitpocketer · May 04, 2015 at 03:32 PM 0
Share

Yes, but is your class public? It's not enough to just have public members, there needs to be the 'public' modifier on the class itself (which is easy enough to forget when you make a new class by hand in the editor.

9 Replies

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

Answer by Peter G · Apr 25, 2011 at 12:01 AM

First, if you intend to let someone edit these values in the inspector, then they have to be public... and you only get this error when you have private fields so you might have another problem. Make sure you note that unlike Javascript where the default access modifier is public, in C# you have to put public before your variables.

You can explicitly declare the reference null as described here. And that is a proper way of fixing it.

 private MyReferenceType field = null;

Or, I hesitate to show this because someone will abuse it, but you can tell the compiler to ignore warnings.

//Top of the script
#pragma warning disable 0649

You really should use the first method though.

Comment
Add comment · Show 10 · 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 microp · Apr 25, 2011 at 01:26 AM 1
Share

Initializing the fields to null (oddly) did do the trick. Thanks for that!... and also for making me realize that the error codes are the same as $$anonymous$$icrosoft's. That's gonna save you guys a few dumb questions! :P

avatar image Mockarutan · Jul 08, 2015 at 07:18 AM 21
Share

"First, if you intend to let someone edit these values in the inspector, then they have to be public". No, this is a common error by Unity users. The 'public' syntax is used, as I think you know, to let other classes access a variable. Unity just happens to serialize public variables by default. If you have variables in a class that acts as settings that is changeable in the editor, these variables should be private and [SerializedField] because no other classes should access these variables, right? (Rhetorical question mark)

avatar image iFischer_d · Aug 24, 2015 at 07:55 AM 8
Share

$$anonymous$$rCranky: Regardless of the reflection and attribute used by Unity to expose private member variables, by making them a property changeable in the editor, they are no longer private

This is a very limited view on what is private. By marking the filed as [SerializedField] this keeps access to the member as private within the assembly it's defined. It only elevates it's access to the serialization system as well as editing it's serialization within the Editor assembly.

$$anonymous$$ost professional coders write code that they do not want other people to question how to use or need to read documentation to understand what each field does. example: a serialized dictionary. Unity cannot do this by default, so you would implement 2 private serialized lists (keys and values) that must be kept in sync threw the ISerializationCallbackReceiver. You provide them methods to add, remove, index into this dictionary as well as a custom editor to save this when not running, but you would not want to let them have access to the key list to add and remove (or worse yet, replace) post deploy time.

Using the [SerializedField] attribute you can make a custom editor for this list that will allow them to edit it when not running the project, and not worry about getting bugs back or jeopardizing the integrity of your companies projects from some random bad coder who decides to add a key to the list itself without adding to the values.

avatar image svendkiloo · Dec 10, 2018 at 09:02 AM 5
Share

I would vote down this answer for the first sentence (but my reputation isn't high enough). It is completely wrong (as also mentioned by other commenters), and I think it's very unfortunate that it's listed as an accepted answer here, as it will spread the bad practice of making fields public to serialise them. They should only be made public if they need to be accessed from other classes (and even then it would be better practice to add public properties to do that (especially if only read access is required).

avatar image daimadoushi · Jul 24, 2019 at 05:33 AM 3
Share

All these answers are wrong !

You shouldn't put every variable to public just because you want to mess with them in the inspector. The proper way to do so is to add a [SerializedField] in front of the private field. Although that will not solve the warning issue.

I am still looking for an answer to suppress those pesky warnings in a good way though ...

avatar image Pablomon daimadoushi · Apr 10, 2020 at 02:59 PM 0
Share

I $$anonymous$$ostly and Absolutely agree to your comment dhaimadoshi.

avatar image neo-mashiro daimadoushi · Nov 09, 2020 at 12:59 AM 0
Share

The only good way is to initialize the field to 0 or null, that's it, no #pragma stuff. Unless you can tell the compiler that field is initialized via reflection, there's no better solution.

Show more comments
avatar image
2
Best Answer

Answer by Bunny83 · Apr 24, 2011 at 11:14 PM

That warning should only appear for private member variables. Those can't be set from outside and that's why the compiler can say for sure that they aren't set anywhere. In C# a variable without an access modifier (private,protected,public...) is always private. To be able to access the variable in the inspector or from another script you have to make it public. The warning should disappear then.

Comment
Add comment · Show 8 · 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 microp · Apr 25, 2011 at 12:59 AM 0
Share

Riiight right right. I did read about that somewhere. Some other scripts don't complain as such, so I suppose it slipped my $$anonymous$$d during the conversion process.

Wait, I still get the warnings after making the fields public. That's odd. I'll check it out and brb.

avatar image microp · Apr 25, 2011 at 01:04 AM 0
Share

Ah there you go. I just had to restart Unity and now they're gone. Thanks!

avatar image microp · Apr 25, 2011 at 01:15 AM 0
Share

Oh, nope. They're back. Even if I set all such public fields in the editor. But it doesn't do it to all my scripts. Still looking into it.

avatar image Bunny83 · Apr 25, 2011 at 01:25 AM 0
Share

That's kinda impossible. This warning can only appear for privat (or maybe even for protected) members. If they are public they can be accessed from everywhere and unity can't say if it's used or not and therefore don't throw a warning. Are you sure you removed your old JS version of that class? $$anonymous$$aybe post the declaration of one of your variables that throw that warning and copy the exact warning message.

avatar image Bunny83 · Apr 25, 2011 at 08:04 AM 0
Share

Just take a look at this page: http://msdn.microsoft.com/en-us/library/03b5270t%28v=vs.71%29.aspx

Show more comments
avatar image
48

Answer by Delacrowa · Mar 30, 2019 at 03:34 PM

Just in case you don't want to mess up with pragmas, ruin incapsulation and Unity best practices, and/or kill ALL "Field [Name] is never assigned to, and will always have it's default value null" as there can be many useful, then I have one solution. It is best for me now as it covers also store assets. You should replace all

 [SerializeField] private GameObject _value;

with

 [SerializeField] private GameObject _value = default;

I assume you're using new C# 7.1 in this case. And to make all process automatic and quick ASAP here are regEx for VS for replacement. Press Ctrl + Shift + F or your shortcut to open "Find and replace" window and fill it with:

Find what: (\[SerializeField\]([^=]+?));

Replace with: $1 = default;

Make sure Use Regular Expressions is turned on.

Something like this screenshot

This can be done each time you download/update store asset. Enjoy and vote this post to let others know about this solution.

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 svendkiloo · Apr 01, 2019 at 07:09 AM 0
Share

This is also the solution we've adopted after updating to more recent Unity/C#. Nice find-replace query :)

avatar image bibbis · Apr 04, 2020 at 01:48 PM 0
Share

VERY good, thank you so much!

I have 2 questions about this though.

  1. Why is this not done automatically? Unlike C++ where an uninitialized float will be any random value previously in memory, in C# it will very specifically be 0. Am I missing something here or will this not give the same result? private float _f; private float _f = 0f; private float _f = default;

  2. Why are private variables with the [SerializeField] attribute not being excluded from this warning?

Thanks again!

avatar image
5

Answer by Ashkan_gc · Aug 01, 2012 at 09:24 AM

This warning is notifying you of a variable which you never assigned a value to and it's not possible to assign a value to it from any other script. When you create your scripts within unity itself or the monodevelop IDE then the generated code assigns no access modifiers (public/private/internal/protected) to your class and the default access modifier in C# is private so you class will become private and it's public variables are not accessible from other scripts. You can assign them from inspector but the compiler has no way of understanding it. Unity can check it's serialized data for the script and filter the warning in console based on the assignment of the value in the inspector but it doesn't. You have different ways to solve it.

  1. Make the class public and the warning will disapear.

  2. Assign null or something else to all public variables by default

  3. write #pragma warning disable 649 on top of your script.

The third method is not good because it will not worn you about your other illegal uses and other variables that you wrongly defined and never assigned. The first method is not good from a software engineering point of view because you are giving access to a class that is not required to be accessed from outside world. The second method makes the code ugly and you sometimes might forget assigning the value but seems the best approach.

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 MrCranky · Mar 15, 2013 at 10:49 AM 0
Share

I wish I had the reputation to up-vote this, as it's your point 1. that is almost definitely the correct solution here. The warning should go away when you make the members public, and if it isn't, it'll most likely be because the class has to be public as well. The auto-generated $$anonymous$$onoBehaviour classes are always public by default, but if you're creating from an empty file or porting code from another language it's very easy to miss that out.

Explicitly disabling the warning is never the solution, it's just hiding the symptom. And while assigning sensible defaults is usually a good thing, in this odd hybrid case where the Unity Editor is responsible for assigning values, setting references to null explicitly everywhere is against the typical convention. $$anonymous$$ost people co$$anonymous$$g to this question will be really asking: "why is this warning co$$anonymous$$g up for this class, and not all these other classes where I don't have to assign null explicitly?" The answer most likely is: "because both the class and the member variables need to be public"

avatar image
2

Answer by Ashkan_gc · Jun 08, 2011 at 03:07 PM

i don't know why but unfortunately unity generates cs0649 even for public variables in ScriptableWizards. for disabling the warning just assign null to the variable or use #pragma warning disable 649

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 jmasinteATI · Aug 24, 2015 at 12:20 AM 0
Share

Yep, ran into the same thing...a public variable I had to set to null to make the message go away.

  • 1
  • 2
  • ›

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

20 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

Related Questions

Schrödinger's warning with MonoBehavior camera member (whether new keyword is used or not) 1 Answer

How I can change script of MouseLook, Use GetComponent() instead. 1 Answer

Warning messages lagging game 0 Answers

Animator Component has a warning dialog in play mode that result in making a humanoid character in T pose 0 Answers

How to fix error for first personal controller? 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