Search Unity

[SerealizeField] field does not get serialized in build

Discussion in 'Scripting' started by th0m4sXD, Aug 26, 2019.

  1. th0m4sXD

    th0m4sXD

    Joined:
    Oct 22, 2017
    Posts:
    9
    I have prefab with script which has a serialized field. Reference to this field is assigned in the editor but for some reason this field is null in instantiated object in runtime in built version. In the editor play mode its okay.

    Code (CSharp):
    1. public class MyScript: MonoBehaviour
    2. {
    3.     [SerializeField] private SomeScript serializedField;//originaly UnityEngine.UI.Text type
    4.  
    5.     public void MyFoo()
    6.     {
    7.         //temporary fix
    8.         if (!serializedField)
    9.             serializedField= GetComponent<SomeScript >();
    10.  
    11.          serializedField.Foo();//null reference exception
    12.     }
    13. }
     
  2. ThySpektre

    ThySpektre

    Joined:
    Mar 15, 2016
    Posts:
    362
    Having trouble following this. Are you defining member Foo anywhere?
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It's pretty weird that this would give different behavior in a standalone build than when playing in the editor. I'm not sure what would cause that, but a couple of possible sources of confusion that I would check:
    • A prefab instance in your scene can have different settings than the prefab itself. Don't assume they're the same unless you've checked.
    • Prefabs cannot have references to objects in your scene, only to other parts of the same prefab. (If "SomeScript" is a part of the prefab, though, then that shouldn't be the issue.)
    • Is "SomeScript" a MonoBehaviour, ScriptableObject, [Serializable] type, or similar? Any chance it could be destroyed?
    • Carefully check if your execution path is different in a standalone build than when playing in the editor. For instance, maybe the standalone build has a "loading" scene before the main scene is loaded that you might be skipping while in the editor? Try to make these execution paths match as closely as possible for testing purposes.
     
    Dextozz likes this.
  4. th0m4sXD

    th0m4sXD

    Joined:
    Oct 22, 2017
    Posts:
    9
    Because i did build in unity cloudbuild the problem was solved by setting exactly the same unity version in cloud build config as on my pc.