Skip to main content
Changed from suggesting public fields, to [field: SerializeField] as it is a great shorthand not many seem to know :)
Source Link

It was due to having auto properties on serialized classes:

public TileData TileData
{
    get;
    private set;
}

Using properties with [SerializableField][SerializeField] private fields fixes it:

[SerializeField]
private TileData tileData;
public TileData TileData
{
    get { return tileData; }
    private set { tileData = value; }
}

Or if you are lazy, just use public members[field: SerializeField]:

[field: SerializeField] public TileData TileData;TileData {get; private set;}

(But it's against some design principles, so obviously try not to use them.)

UPDATE:

  • It was still losing references,

  • solve it by calling EditorUtility.SetDirty() before AssetDatabase.SaveAssets()

It was due to having auto properties on serialized classes:

public TileData TileData
{
    get;
    private set;
}

Using properties with [SerializableField] private fields fixes it:

[SerializeField]
private TileData tileData;
public TileData TileData
{
    get { return tileData; }
    private set { tileData = value; }
}

Or if you are lazy, just use public members:

public TileData TileData;

(But it's against some design principles, so obviously try not to use them.)

UPDATE:

  • It was still losing references,

  • solve it by calling EditorUtility.SetDirty() before AssetDatabase.SaveAssets()

It was due to having auto properties on serialized classes:

public TileData TileData
{
    get;
    private set;
}

Using properties with [SerializeField] private fields fixes it:

[SerializeField]
private TileData tileData;
public TileData TileData
{
    get { return tileData; }
    private set { tileData = value; }
}

Or if you are lazy, just use [field: SerializeField]:

[field: SerializeField] public TileData TileData {get; private set;}

UPDATE:

  • It was still losing references,

  • solve it by calling EditorUtility.SetDirty() before AssetDatabase.SaveAssets()

added 144 characters in body
Source Link
Tudvari
  • 771
  • 13
  • 44

It was due to having auto properties on serialized classes:

public TileData TileData
{
    get;
    private set;
}

Using properties with [SerializableField] private fields fixes it:

[SerializeField]
private TileData tileData;
public TileData TileData
{
    get { return tileData; }
    private set { tileData = value; }
}

Or if you are lazy, just use public members:

public TileData TileData;

(But it's against some design principles, so obviously try not to use them.)

UPDATE:

  • It was still losing references,

  • solve it by calling EditorUtility.SetDirty() before AssetDatabase.SaveAssets()

It was due to having auto properties on serialized classes:

public TileData TileData
{
    get;
    private set;
}

Using properties with [SerializableField] private fields fixes it:

[SerializeField]
private TileData tileData;
public TileData TileData
{
    get { return tileData; }
    private set { tileData = value; }
}

Or if you are lazy, just use public members:

public TileData TileData;

(But it's against some design principles, so obviously try not to use them.)

It was due to having auto properties on serialized classes:

public TileData TileData
{
    get;
    private set;
}

Using properties with [SerializableField] private fields fixes it:

[SerializeField]
private TileData tileData;
public TileData TileData
{
    get { return tileData; }
    private set { tileData = value; }
}

Or if you are lazy, just use public members:

public TileData TileData;

(But it's against some design principles, so obviously try not to use them.)

UPDATE:

  • It was still losing references,

  • solve it by calling EditorUtility.SetDirty() before AssetDatabase.SaveAssets()

Source Link
Tudvari
  • 771
  • 13
  • 44

It was due to having auto properties on serialized classes:

public TileData TileData
{
    get;
    private set;
}

Using properties with [SerializableField] private fields fixes it:

[SerializeField]
private TileData tileData;
public TileData TileData
{
    get { return tileData; }
    private set { tileData = value; }
}

Or if you are lazy, just use public members:

public TileData TileData;

(But it's against some design principles, so obviously try not to use them.)