- Home /
Referencing a ScriptableObject asset programatically
I've created a custom editor for a MonoBehaviour that I'm working on and have run into a small problem concerning ScriptableObjects...
In the Inspector, the user may press a button which auto-generates a PostProcessVolume from Unity's PostProcessStack. This PostProcessVolume requires a PostProcessProfile which is a ScriptableObject.
So I've created the profile and I've saved it as an asset.
PostProcessProfile profile = ScriptableObject.CreateInstance<PostProcessProfile>();
AssetDatabase.CreateAsset(profile, "Assets/PostProcessProfile.asset");
This is all fine however if I feed profile directly into a PostProcessVolume, it appears in the inspector as "Profile (Instance)" which I don't want. I want it to be a direct reference to the saved asset. This causes issues and the instance is removed after one play-time and certain NullPointer errors keep appearing so I cannot just leave it like this.
I've tried loading the asset and using that as below, however I think it is creating another instance by doing so which isn't what I want.
volume.profile = AssetDatabase.LoadAssetAtPath<PostProcessProfile>("Assets/PostProcessProfile.asset");
So my question is, is there any way I am able to get a reference from a ScriptableObject that has been saved as an actual asset? At the moment, it seems my only option is to ask the user to actually select the profile in the inspector which I don't want to have to do. Surely the reference can be made in an editor script?