• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by turbolek · Dec 03, 2019 at 12:39 PM · editorobjecteditor-scriptingserialization

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image turbolek · Dec 03, 2019 at 01:50 PM 0
Share

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.

avatar image KaspianR turbolek · Dec 03, 2019 at 02:24 PM 0
Share

Oh! Then I learnt something new too! Glad it worked out for you and good luck with the project!

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Serialization of List 2 Answers

how can I create a new Object from custom Editor 0 Answers

UnityEvent Serialization Problem 1 Answer

Storing data about an asset only in the editor 2 Answers

Editing prefab fields from custom editor that are not automatically serialized 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges