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!

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.

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.

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.

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

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.

This is a complicated issue as detailed at the link below. As you’re probably experiencing issues with modern C# design patterns that mark a serialized property as private.

https://forum.unity.com/threads/warning-cs0649-not-suppressed-properly-when-field-is-marked-as-serializefield.556009/

You have two solutions. The first and in my opinion best is to globally disable the warning till Unity finds a good solution. You can easily do this by adding a file to Assets/csc.rsp with the following contents.

-nowarn:0649

As an alternative if you don’t want to disable legit 0649 warnings. You can initialize your variables with a default value. This suppresses the warning by setting a value the compiler can read. But it will create unnecessary clutter in your code.

[SerializeField] 
private string _scenePath = default;

Another option that I always use in my projects is to add csc.rsp file into the Assets folder. You can open up a new notepad file, drop the line -nowarn:0649 into it and save as csc.rsp.
This way, any time you use

[SerializeField]
private string _myString;

or the like, to be assigned in the inspector, you will no longer get any warnings in the Unity console.
This solution is essentially the same as the #pragma warning disable 0649 one, but you only need the single file to apply to all of your scripts instead of adding that line to each and every script. Any time I expose a field in the inspector, it will be a private serialized field, making the csc.rsp a default in any of my new Unity projects.

I had this same issue, and the problem was that my class didn’t had the public modifier when being defined. I had:

class MyClass : MonoBehaviour {

}

and fixed it with:

public class MyClass : MonoBehaviour {

}

and apparently in Unity 2017 it still complains even if you put:
#pragma warning disable 0649
or assign it null from the start.
very anoying.

its a combo script in my case (parts run in an editor window parts run in game) and in the editor part things are actually assigned but for some reason unity doesn’t see it and complains, tried disabling the warning with pragma no go, tried GameObject go = null; and no go unity still complains that it has its default value of null, so not sure what unity did in the 2017 release