Reahreic
Reahreic

Reputation: 629

Property appears to be self initializing when running in unity editor

I've got a strange issue, that makes no sense. It appears that one of my variables that store a custom class is self initializing when it should be staying null. I've deleted 99% of the code in the two class files involved and simplified the remaining to its minimum while debugging, yet it still occurs.

The code is in question is designed to [ExecuteInEditMode] in the background in Unity while the designer does their thing and not in play.

  1. On the first tick, ownedData evaluates as expected (null).
  2. On the second tick, ownedData is evaluating as if an object was assigned even though I ensure that canPrepare is never set to true. (Its internal properties are still null though)
[Serializable]
public class MyData {
    public byte[] id { get; private set; } = null;

    public MyData(byte[] id) {
        this.id = id;
    }
}

#if (UNITY_EDITOR)
    [ExecuteInEditMode]
#endif
public class Owner : MonoBehaviour {
    private bool canPrepare = false; //Made private to ensure nothing external can set this to true
    private MyData ownedData = null;

#if (UNITY_EDITOR)
    private float _editorTime;
    public void Awake() {
        EditorApplication.update += EditorDataEval;
    }

    private void OnDestroy() {
        EditorApplication.update -= EditorDataEval;
    }

    private void EditorDataEval() {
        if (Application.isPlaying) { return; }

        if (Time.realtimeSinceStartup - _editorTime >= 1f) {//Tick once per #f seconds
            _editorTime = Time.realtimeSinceStartup;

            if (ownedData == null) {
                if (canPrepare) {
                    ownedData = new MyData(System.Guid.NewGuid().ToByteArray());
                }
                return;
            } else {
                //Should never be called as canPrepare is not set
                Debug.Log("Data id: "+ ownedData.id);
            }
        }
    }
#endif

//TOOD: Add class methods back in once bug resolved.
}

I'm starting to theorize that Unity is in the background auto initializing the variable in order to show it in the editor...

Upvotes: 0

Views: 1020

Answers (1)

derHugo
derHugo

Reputation: 90659

By default this should and does not happen since your field is not public and not [SerializeField].

I guess you once entered the Debug mode in the Inspector:

In this case the Unity Inspector will automatically instantiate the field because its type is [Serializable]! Later it stays instantiated until the scene is re-loaded even if you already left the Debug mode.

As you can see not even Reset does help - I can only guess but I think Unity only resets the serialized Inspector fields here.

A really quick fix would be to simply enter and exit PlayMode once. The scene and thereby your component is reloaded and the field reset to null.

Another option would be to implement the Reset method and actually do it there

private void Reset()
{
    ownedData = null;
}

Now if you try it again and hit Reset in the contextMenu you can see that the output is stopped since the field is reset to null.

enter image description here

Upvotes: 2

Related Questions