Editorscript: How to reference prefabs in editor not in awake or start
I have an editor script that instantiates prefabs, this script works perfectly but I wanted to be a bit more specific and reference my prefabs in code instead of having to drag a new prefab to the inspector every time. I try to reference a prefab at the beginning of my class like this :
Object intersection = EditorUtility.InstanceIDToObject(-70696);
By doing this I get the error:
"InstanceIDToObject is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'ObjectBuilderScript' on game object 'south'. See "Script Serialization" page in the Unity Manual for further details. UnityEditor.EditorUtility:InstanceIDToObject(Int32) ObjectBuilderScript:.ctor()"
I read the page on Script Serialization but didn't manage to find information on how to do this. I have tried different methods but there doesn't seem to be an alternative to reference my prefabs directly in editor.
Did you ever figure this out? I was wondering the same thing now. I think Unity might have some way to iterate over the AssetDatabase and check the exact path of each asset.