- Home /
How to assign object reference to ObjectField knowing the object's name?
I have a custom editor script with an ObjectField where I can assign my custom implementation of ScriptableObject. After I assign a scriptable object I get its name and store it so it persists when I reboot Unity. Knowing the object's name can I reassign the value to the ObjectField after rebooting Unity?
Answer by KaspianR · Dec 03, 2019 at 01:20 PM
If the path in your project folder is persistent you can use that and the name of the object, otherwise I would store the path instead of the name since the name can always be received from the path in case you need it and that's just faster, better and easier in all ways and honestly, using a text object or most other methods really would probably suit you better. But if you still prefer to have it only save the name, here is a simple example of a script that loads a ScriptableObject by it's name (be it alot more resource intensive than storing the path itself):
ScriptableObject so = (ScriptableObject) AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("NAME OF SCRIPTABLE OBJECT")[0]));
Obviously replace the "NAME OF SCRIPTABLE OBJECT" with the actual name of the object and “ScriptableObject” with the name of your ScriptableObject class. Hope this helps you and good luck!
Thanks, that does the trick. Also it turns out that Resources.Load works in the editor the same way it does in playmode, so i could be used as well.
Oh! Then I learnt something new too! Glad it worked out for you and good luck with the project!
Answer by Bunny83 · Dec 03, 2019 at 01:16 PM
This makes no sense. The name of things are not necessarily unique in the first place. Also if you have a public / serialized field that references your scriptable object asset, that reference would be serialized as well if you created your "editor script" correctly. However since we don't know what your editor script does, how and where you show your object field and where you want to store that reference we can not really help you here.
To directly answer your question: No there's no built-in method to find an arbitrary asset by name. Apart from that when you build your game only used assets are included in the build. If the scriptable object is not (actually) referenced from an included scene (or a dependency of the scene) or placed inside the resources folder it won't be part of the build.
To me that sounds a lot like you are not really familiar with Unity's serialization system and how the editor works. Since you have a specific problem you should post all relevant information on that problem. You just came up with a strange workaround-approach which just doesn't work and asked how this can be done. Tell us what you actually want to do, what you currently do (include the relevant code, and where it's called from) and what exactly isn't working.
Your answer
Follow this Question
Related Questions
Editing prefab fields from custom editor that are not automatically serialized 0 Answers
Custom inspector variable resets automatically 1 Answer
Dynamic serialized fields based on enum 0 Answers
Storing data about an asset only in the editor 2 Answers
Help with Missing Monobehaviours and Asset Serialization? 0 Answers