- Home /
[SerializeField] private Vector3[] array works in editor/doesn't work in stand-alone build
I have class like
[ExecuteInEditMode] public class Trajectory : MonoBehaviour { [SerializeField] private Vector3[] points;
private void Awake() { Debug.Log(string.Format("Adding {0} saved points to trajectory.", points.Length)); } }
I assign array in the Editor, it contains value when I click "Play", but doesn't work in Windows/Mac Standalone builds. In Mac the log is:
Adding 0 saved points to trajectory.
In Windows:
Adding 1 saved points to trajectory.
I'm using Unity 3.2. Any ideas?
I assume you're not in play mode while adding the points (they would disappear when you stop playing). I wonder if [ExecuteInEdit$$anonymous$$ode] somehow fools Unity into thinking you're in play mode and therefore changes shouldn't be saved? Complete speculation, but try removing ExecuteInEdit$$anonymous$$ode and see if it makes any difference.
I add points using class derived from Editor. The situation was that I added points, click Play and saw result as trajectory. Ok, points are saved. But when I compiled stand-alone application (e.g. for Windows), I got no points. This is was due to conditionally serialized field before my serialized array(((
Answer by Anton Grigoryev · Feb 18, 2011 at 05:39 PM
I think I've found the answer. In my code there was conditionally serialized field before my array:
[ExecuteInEditMode] public class Trajectory : MonoBehaviour { #if UNITY_EDITOR
[SerializeField] private bool smart;
endif
[SerializeField] private Vector3[] points;
private void Awake() { Debug.Log(string.Format("Adding {0} saved points to trajectory.", points.Length)); } }
Data was saved in editor, but in stand-alone there is no field 'smart' and boolean value 0 or 1 (if smart was set to false or true) as size of the Vector is loaded.
Do you have any idea how to improve the code above to prevent such errors in the future? Or maybe some related information? I'm using this flag only in editor to edit trajectory. How can I refactor the code?
The Unity serialization process deals with fields being added and removed. I had assumed this would work for conditionally compiled fields as well, but perhaps not. So I would not expect the value of "smart" to be deserialized into "points". But I could be wrong -- just yesterday I added some #if UNITY_EDITOR fields to my project as well, so I'd like to know the answer!