I am trying to track down an intermittent bug that is driving me nuts.
I have a bit of code that is executed inside of a ScriptableObject asset. This code creates a subasset that is also a SO.
Simplified and sanitized code below:
public T CreateProperty<T>()
{
T newProperty = ScriptableObject.CreateInstance<T>();
AssetDatabase.AddObjectToAsset(newProperty, this);
// .... snipped
}
Now the weird bug is that occosionally, and best I can tell completely randomly, when the AddObjecToAsset method runs, Unity will throw an error as shown below, but the super confusing part of this error is it is essentially claiming that the object running the code, “this” has been destroyed. Which seems impossible since it is the object running the code! Furthermore it still exists as I can see the asset in the project window.
Error: MissingReferenceException: The object of type ‘MainAsset’ has been destroyed but you are still rying to access it.
Now I know it is referring to “this” and not “newProperty” based on the type indicated in the error (which is not MainAsset in reality but that illustrates the point).
Anyone seen anything like this? It is driving me crazy.