CS0649 warnings on uninitialized public fields

So this is a C# warning I get in Unity but not in VS, and only with some of the scripts I wrote with purposefully uninitialized public fields (not even all of them!).
Assets\Scripts\Tabs\Takeout\AddPlace.cs(7,24): warning CS0649: Field 'AddPlace.key' is never assigned to, and will always have its default value null

using UnityEngine;
using UnityEngine.UI;

class AddPlace : MonoBehaviour
{
    public InputField input;
    public string url, key;
    public TakeoutLoader loader;

    public void OnClick()
    {
        loader.postRequest = true;
        loader.url = url;
        loader.formData = new JSONLoader<Takeout>.Field[] { new JSONLoader<Takeout>.Field(key, input.text) };
        input.text = string.Empty;
        loader.Refresh();
        loader.formData = new JSONLoader<Takeout>.Field[] { };
    }
}

As far as I know, Unity shouldn’t mind missing initializations, as long as the fields in question are serialized…
Also, I find it particularly weird that this happens only with the very last C# scripts I’ve written so far. Could there have been some troubles with their importation/linking to the project?
Thanks in advance!

what happens if you declare the class public? that’s the only way I’ve ever used unity.

what happens if you put the url and key declarations each as their own statement? again, that’s the only way I’ve ever done public inspector variables in unity.

It works now. Thanks a lot! I completely forgot about that.

1 Like