Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

(Case 902037) Cannot build player with partial classes sometimes

Discussion in '2017.1 Beta' started by Dizzy-Dalvin, Apr 16, 2017.

  1. Dizzy-Dalvin

    Dizzy-Dalvin

    Joined:
    Jul 4, 2013
    Posts:
    54
    I have a partial class with three parts and very often when building in 2017.1.0b1 I get the following errors:

    Code (CSharp):
    1. Fields serialized in Editor, class 'Player'
    2.     'FieldOne' of type 'System.Int32'
    3.     'FieldTwo' of type 'System.Int32'
    4.     'FieldThree' of type 'System.Int32'
    5.  
    6. Fields serialized in target platform, class 'Player'
    7.     'FieldTwo' of type 'System.Int32'
    8.     'FieldThree' of type 'System.Int32'
    9.     'FieldOne' of type 'System.Int32'
    10.  
    11. Error building player because script class layout is incompatible between the editor and the player.
    It's clear the order of the fields is changing somehow between the editor and the build target.
    I have filed a bug (case 902037) but the problem is I cannot find clear steps to reproduce it and I fear the QA would reject it. Sometimes you just open the project, try to build and get the error, and sometimes it doesn't happen at all. Sometimes it goes away until you modify the source file (seems to work only with the main one out of the three).
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    I'm afraid this is not something we can easily fix. The problem with it is that the C# compiler can decide the order in which it lays out fields in partial classes, and our serialization system depends on the fact that the order is the same in the editor and in the build.

    An easy workaround on your side would be to put all serializable fields into a single file of the partial class.
     
    Peter77 likes this.
  3. Dizzy-Dalvin

    Dizzy-Dalvin

    Joined:
    Jul 4, 2013
    Posts:
    54
    But what makes the compiler behave differently when serializing for the build and for the editor? Wouldn't the order also change between editor sessions if it were decided solely by the compiler?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    It could be multiple things, including the fact that on some platforms we use a different C# compiler.