# Why does the debug say my assigned field “is never assigned to, and will always have its default value 'null'”?

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);

Item itemTwo = new Item(1, "Item 2", itemTwoSprite);

Item itemThree = new Item(2, "Item 3", itemThreeSprite);
}
}


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.

• It is actually funny, that "fix" to that error is to explicitly set the field to 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. – wondra May 16 '17 at 8:04
• I think, this problem occurs when your IDE compiles the C# project. It's not aware that you can assign this private field through an external application (Unity Editor) and Unity retrieves the warnings of the IDE compiler (plus, the ones Unity itself generates) – Hellium May 16 '17 at 9:28

I feel it is important to note that I can not replicate this problem. However, this really isn't a problem. Your being told that there might be a problem - and that you should address it now, instead of later; if we know that it is not a problem, this is a non-event.

### What are the yellow exclamation marks?

In Unity, logs marked by a yellow exclamation mark are not errors; they are simply warnings. It is the editors way of letting you know that there may be a problem - they pose any issues, by themselves. In this case, it can see that the code does not allow for such references being set; but your providing the references via inspector, so it is not a problem.

As far as I am aware, there is a #pragma line you can include in your script to prevent warnings associate with this script; however, this line prevents all warnings, and is a general bad idea. These warnings can often be very useful. I will not go into it, simply for how dangerous it might be, if you are not completely familiar with your code and all possible outcomes. You can omit them from your debug log, in general, by deselecting the matching exclamation mark symbol to the top right.

In this case, it also highlights a generally useful practice; especially in regards to anything that is set via the Inspector: it might be useful to make sure that your reference has been set, prior to accessing it. That way, at times where the reference has not been set, it is much easier to diagnose. Having used Unity for several years, I can personally vouch for a couple of excruciating painful errors that came down to a reference I was sure I had set, or otherwise became unset due to a change in the script or object.

if(itemOneSprite != null)
{
Item itemOne = new Item(0, "Item 1", itemOneSprite);