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!
Try just initializing them to blank strings "" and what not?
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.
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.
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.
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.
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
"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)
$$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.
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).
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 ...
I $$anonymous$$ostly and Absolutely agree to your comment dhaimadoshi.
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.
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.
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.
Ah there you go. I just had to restart Unity and now they're gone. Thanks!
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.
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.
Just take a look at this page: http://msdn.microsoft.com/en-us/library/03b5270t%28v=vs.71%29.aspx
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.
This is also the solution we've adopted after updating to more recent Unity/C#. Nice find-replace query :)
VERY good, thank you so much!
I have 2 questions about this though.
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;
Why are private variables with the [SerializeField] attribute not being excluded from this warning?
Thanks again!
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.
Make the class public and the warning will disapear.
Assign null or something else to all public variables by default
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.
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"
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
Yep, ran into the same thing...a public variable I had to set to null to make the message go away.