I have an itemData
class that stores the data for a couple of items. It looks something like this:
public class ItemData : MonoBehaviour
{
public List<Item> items;
[SerializeField] private Sprite itemOneSprite, itemTwoSprite, itemThreeSprite;
void Awake()
{
items = new List<Item>();
Item itemOne = new Item(0, "Item 1", itemOneSprite);
items.Add(itemOne);
Item itemTwo = new Item(1, "Item 2", itemTwoSprite);
items.Add(itemTwo);
Item itemThree = new Item(2, "Item 3", itemThreeSprite);
items.Add(itemThree);
}
}
I get an error message for itemTwoSprite
and itemThreeSprite
, stating that they are null and will always be null:
Field name of field is never assigned to, and will always have its default value 'null'
Oddly, when I check the inspector, those fields are not null. I have manually attached sprites to them. When I debug, I see that they are not null. I get something like "itemTwoSprite(UnityEngine.Sprite)". I can run the game, and things seem to work normally.
The order of the declared Sprite variables seems to be important, as well. For instance, if my line of code looked like Sprite itemTwoSprite, itemOneSprite, itemThreeSprite
, then itemOneSprite
and itemThreeSprite
would bring up error messages. The first one declared seems to be unproblematic.
What could be causing the issue, and how do I fix it? I have isolated the problem into an empty project, and it still persists. The only other script is the item class. To reiterate, the error message does not prevent the game from running, but it pops up in the console from time to time, with the yellow exclamation mark.
null
i.e.[SerializeField] private Sprite itemOneSprite = null;
I personally find that solution better than suppressing all of the CS0649, because some of them might be actual problems. \$\endgroup\$