Search Issue Tracker

Active

Planned for 2019.4, 2020.1

Votes

166

Found in

2018.3.0a1

2018.3.0b1

Issue ID

1080427

Regression

Yes

[SerializedField] fields produce "Field is never assigned to..." warning

Scripting

-

1. Open the attached project (warning.zip)
2. Observe the warning in the console

Expected: field with SerializeField attribute do not produce the warning

Reproduced in 2018.3.0a1, 2018.3.0b1, 2018.3.0b2, 2019.1.0a1
Did not reproduce in 2018.2.8f1
Regression introduced in 2018.3.0a1

  1. Response avatar

    lukaszunity

    Nov 06, 2019

    #pragma warning disable 0649
    // your code
    #pragma warning restore 0649

    Or disable it globally by adding a file named csc.rsp to the root of you Assets folder, e.g. Assets/csc.rsp, with the following contents without quotes.

    /nowarn:0649

    This will tell the C# compiler to not emit a warning for CS0649 / unused fields.

    If you have .asmdefs with csc.rsp in your project, then you also need to add the "/nowarn:0649" line to those csc.rsp files.

Comments (76)

  1. 41b02dbd30f08d896b361aabf9fa7032?d=mm

    Artifact-Jesse

    May 25, 2019 05:41

    There is no way this is By Design... Please reopen this issue. It's driving me nuts.

  2. C35b9264646fd1b72f6ccfca2622c409?d=mm

    AlanMattano

    May 08, 2019 15:27

    Can Unity make a video about this .Net 4.X problem?

  3. Dd8da009afba747778db5dcb5866de9e?d=mm

    Alexees

    Apr 26, 2019 12:49

    What makes this unnecessarily tedious is the fact that you can use "= default" for every field.

  4. 3e2ae4101faf1b19afa71b4540d5dac6?d=mm

    MrDude

    Apr 21, 2019 06:10

    By design, declare your private fields public.
    That makes total sense. why is anyone complaining about this?

  5. Cb0088335925614ac61657710cac4207?d=mm

    Gillissie

    Apr 16, 2019 18:00

    While it's true that it's bad design for these warnings to show up, it's also bad practice to declare inspector properties without initializing them with default values. Initialize your inspector property variables with default values to eliminate the warnings.

  6. Ba5acdfbdf2a33ddaae2e55ebdb45293?d=mm

    yc960

    Apr 13, 2019 13:47

    very poor design

  7. 162782902a85928256d1d1e7b617ab78?d=mm

    roberto_sc

    Apr 11, 2019 05:52

    This is not a good solution.

  8. F9440774552a4e4217bc66136867b935?d=mm

    Lyje

    Apr 07, 2019 11:58

    @Xavier78 I strongly disagree. In my view it's almost always bad practice to assign to [SerializeField] fields from code. These fields are for data to be injected by Unity and in most cases should be essentially immutable from the code's perspective.

    Given this - which is of course an opinion, but one that has served me very well - the warning actively discourages good practice.

  9. D9105352c7a09434ff8fb69b76eb1ad9?d=mm

    Xavier78

    Mar 21, 2019 02:09

    Okay so I have been dealing with this for a while. The warning should still be there, and this is why. If a private field has a property, or gets assigned to from a public method, this warning will go away. If a private serialized field doesn't have one of those two things then it is bad. Clearly that variable needs to be assigned to by hand since you are showing it to the editor, so there should be a way to assign to it from code side as well. I would suggest if you don't want to use prop, then use a Setup() method that has all serialized fields assigned to, which will make your code more suitable for unit testing in the future. I sincerely hope they do not get rid of this warning for [SerializedFeilds] as that would make wrong coding practices accepted.

  10. 03b5422b519c51eb98f03f25678712d8?d=mm

    azuredown

    Mar 05, 2019 18:27

    So Unity's just going to wait for the compiler to fix it? Pretty disappointing considering JetBrains fixed this problem in Rider a long time ago.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.