Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Forcing a asset's revalidation?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Heptagram064, Nov 21, 2022.

  1. Heptagram064

    Heptagram064

    Joined:
    Feb 22, 2022
    Posts:
    94
    So i am designing a custom editor hierarchy, on which hierarchy items are data in a struct array, put on a scriptable object for safekeeping. One of the data-types inside the struct is list of integers, representing ID numbers 'linking' other hierarchy items to each other.

    When the editor calls the OnValidate() function, everything in the scriptable object is updated accordingly, however this only happens if i manually make a change to it in the inspector. This cause e.g. my 'link items' function to fail after adding new items to the struct array through script.

    i suspect this might be because scriptable objects hate using lists inside of a struct array, but im not sure, since everything works fine after i tweak a dumb thing in the scriptable object using the inspector (causing OnValidate to be fired). (forgive the low gif quality, its a 10mb limit);
    Problem.gif
    PS: The error message is "NullReferenceException: Object reference not set to an instance of an object" when accessing the list within the struct array using List.Contains() and List.Add(). The debug.log is a fired in the scriptable objects OnValidate()

    I was wondering if there was a way of forcing a scriptable object to re-validate using a function in a editor script, since it would solve my issue (something like Validate(object)). Couldn't find anything like it in the docs.
     
    Last edited: Nov 21, 2022
  2. Heptagram064

    Heptagram064

    Joined:
    Feb 22, 2022
    Posts:
    94
    AssetDatabase.ForceReserializeAssets() seems to force a OnValidate() call, hence fixing my issue;
    Code (CSharp):
    1. var update = new string[1] { "Assets/ScriptableObject.asset"};
    2. AssetDatabase.ForceReserializeAssets(update);