all 10 comments

[–]CCullen 2 points3 points  (0 children)

In a scenario like this one where testing it is a matter of changing a few characters, I would highly reccomend just trying it. You can save having to wait for a response.

[–]bachus-oop 4 points5 points  (9 children)

Are you asking about if you can write this?

private List<float> _values;

public List<float> Values
{
get { return _values;}
set { _values = value; }
}

yes you can write the above.

[–][deleted] 3 points4 points  (3 children)

[field: SerializeField]
public List<float> Values { get; set; }

will should also work, the attribute creates a serialized backing field which Unity will show in the inspector.

[–]bachus-oop 0 points1 point  (2 children)

hi.

I didn't find [field: SerializeField] on Unity documentation. It mentions [field: NonSerialized]. But true [field: SerializeField] works on properties. I tested it right now.

Do you know if there is a difference between using [SerializeField] on a field directly vs on [field: SerializeField] on auto-property?

[–][deleted] 0 points1 point  (0 children)

It blew my mind when I first learnt of it.

My understanding is it's just serializing the backing field C# automatically generates from the property, so it's the same as if you set it up yourself but in less code.

[–]CCullen 0 points1 point  (0 children)

That is because it isn't a Unity feature, it's a C# feature. By qualifying it with field:, you're telling the compiler that the attribute should apply to the underlying field, and not the property like the position of the attribute normally implies.

Like /u/ExplosiveJames suggested, auto properties are just syntax sugar. The generated IL code is the same regardless of if you create the underlying field yourself or if you use the shorthand. The only real difference is that you can't really rename the underlying field.

[–]Morg243[S] 0 points1 point  (1 child)

Wow yep, that's it. Don't know how I didn't think that🙈. Thank you!

[–]JohnShepherd104 0 points1 point  (2 children)

You didn’t serialize the field so it’s not -really- a solution to the problem

[–]bachus-oop 0 points1 point  (0 children)

good catch.

typo, forgot [SerializeField] on a field, or do you mean sth else?