Linked Questions

Popular Questions

Unity Editor - C#: Why does my list reset to null OnEnable?

Asked by At

I'm trying to make a custom unity editor and for some reason every time I close the window and open it again, my list resets to null.

I'm trying to save data from a dictionary by separating the keys and values into 2 separate lists OnDisable, and then re-creating the dictionary OnEnable by combining the lists. But every time OnEnable is called I get a null from my lists...

Here's an example of what the code looks like.

public Dictionary<string, string> myDictionary = new Dictionary<string, string>();

[SerializeField]
public List<string> listOfDictionaryKeys;


    private void OnEnable()
    {
        // this always returns null
        Debug.Log(listOfDictionaryKeys.Count);

    }

    private void OnDisable()
    {
        // this successfully saves the keys to the list
        listOfDictionaryKeys = (myDictionary.Keys).ToList();

    }

Does anyone have any ideas why I could be losing my list data? I'm not setting any values in an inspector, they're all being set and saved by code.

Related Questions