0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – wondra May 16 '17 at 8:04
  • 1
    \$\begingroup\$ 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) \$\endgroup\$ – Hellium May 16 '17 at 9:28
1
\$\begingroup\$

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.

enter image description here


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);
    items.Add(itemOne);
}
else
{
    Debug.Log("Warning: itemOneSprite has not been set");
}

As for why it picks up that particular order in that way, I might speculate that it is simply picking up additional in-line members as possible sources of concern. The warning might go away if each declaration is on a new line; but that, in itself, is no particular1 reason to do so. Again, I can not confirm this solution, as I do not have the initial warnings when I load your script into my scene. Unity is sometimes funny, that way. Nothing to be concerned about.

1 Unless you want to, which in itself, is often a good reason.

\$\endgroup\$
  • \$\begingroup\$ I tried one declaration per line with [SerializeField[ before each line and the warnings are gone. It's nice to have some idea what the culprit was despite the fact that it was just a warning. Thanks for the help! \$\endgroup\$ – embracethefuture May 16 '17 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.