Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by 2dank4u · May 18, 2017 at 11:05 AM · serializationscriptableobjectassetdatabaseplaymode

Unity loses association to ScriptableObject after exiting playmode

I wrote a ScriptableObject that can be serialized. I can do all of the fancy things with it, create an Asset of it via the creation menu, edit it with the Inspector or use a custom EditorWindow for easier editing. The ScriptableObject is derived from a generic ScriptableObject and serializes an enum array and 2 ints. In the EditorWindow I use the AssetDatabase to create the Assets if that matters.

And everything seems to be fine. Until I enter and exit playmode.

After that all Assets of the ScriptableObject show an Error:

The Associated script can't be loaded. Please fix any compile errors and assign a valid script.

The interesting thing is this: When I have an Asset selected so that I can see it in the Inspector when I exit playmode it does not get that error. It will still work like nothing happened.

That made me curious. So I forced text serialization and created a few ScriptableObject Assets. I gave them some values and copied the Asset-files as well as the Meta-files somewhere else on disk. Then I entered and exited playmode and compared the pre-Playmode files with the ones in the Assets folder.

There was absolutely no difference.

So my question is, why does this happen and how do I prevent it?

Why does Unity suddenly lose the association to files that haven't changed?

And why doesn't ithappen when I haven't got the Asset visible in the editor?

UPDATE: I just did a bit of trying around and found out some new things:

  1. The association is only lost when I have an asset saved via the EditorScript in the scene.

  2. I can restore the associations simply by having all assets selected when entering playmode

This leads me to think that my saving process is somehow messing with the AssetDatabase in a way that is reversed by what ever unity does when it converts all it's stuff for the engine. So I'm just gonna paste the code I use for saving here:`

 public static void Save(UnityEngine.Object asset, string path, string name=null){
         path = RelPath (path);
         if (name == null)
             name = Path.GetFileNameWithoutExtension (path);
         asset.name = name;
         AssetDatabase.CreateAsset (asset, path);
         EditorUtility.SetDirty (asset);
         AssetDatabase.SaveAssets ();
         AssetDatabase.Refresh ();
     }
Comment
Add comment · Show 6
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 _dns_ · May 18, 2017 at 02:10 PM 0
Share

Hi, to make sure I understand what you do: you create a new ScriptableObject with a context menu, then you modify the content of the ScriptableObject while the game is playing, then you save it at the end of the game ? (Not sure about the last saving action). ScriptableObjects need to be created (using CreateAsset like you did), but they don't need to be saved using CreateAsset. If you want to modify them, during gameplay or with an editor script (EditorWindow or custom Inspector), just do modify the data/members/arrays and use EditorUtility.SetDirty() on the ScriptableObject. Then, when the scene is saved (ctrl+s) or using AssetDatabase.SaveAssets(), the changes will be written to disk.

avatar image 2dank4u _dns_ · May 18, 2017 at 03:37 PM 0
Share

I do everything in the Editor, before entering playmode. I create the script, I edit it and save it with the above code before playmode. I do nothing in playmode and then exit it again. Now the association to the script is lost.

avatar image _dns_ · May 18, 2017 at 03:53 PM 0
Share

In that usage scenario: -to create the asset: add a CreateAsset$$anonymous$$enu attribute just before the class definition

  [CreateAsset$$anonymous$$enu(fileName = "$$anonymous$$yScriptableObject", menuName = "Sub$$anonymous$$enu/$$anonymous$$yScriptableObject")]
     public class $$anonymous$$yScriptableObject : ScriptableObject

and after that right click in the project window to create an asset (but i guess you did that already)

-Alternatively, use ScriptableObject.CreateInstance() to create an object by scrtipt in memory, then AssetDatabase.CreateAsset() to create it on disk (but this operation should be done only once to create the asset, never again)

-Now, modify the asset by an editor script or even in game ($$anonymous$$yFloat = 3.0f; etc...). Use EditorUtility.SetDirty() to notify unity that the asset was modified (and that's all)

-When you want to save the modifications on disk, just like with a scene, use ctrl+s. Even before saving the scene, going to play mode & back should not lose any data or modifications.

-You can still force the save on disk by calling AssetDatabase.SaveAssets() if you want, but never call CreateAsset() on the same asset once more (because it's already created).

avatar image 2dank4u _dns_ · May 18, 2017 at 04:09 PM 0
Share

I use ScriptableObject.CreateInstance() to create a new instance in the script. And I get the same bug even when I don't call CreateAsset multiple times (which by the way just seems to throw an error if the asset already exists and then allows me to continue my script without problems).

avatar image _dns_ 2dank4u · May 18, 2017 at 04:26 PM 0
Share

Ok, must be something else then. Have you tried creating a new project and trying to reproduce the same behavior, it should not be long. Doing this step by step and checking if everything works at each step should help you find the problem. There is some simple source code in unity's tutorial website that also may help you compare with your scripts (https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects).

I suspect this is a simple mistake because the description of what you do seems ok. It's not always possible but if you share more code, maybe me/the community can have other idea of the source of the problem.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

AssetDatabase.LoadAssetAtPath() not working after any sort of project change. 0 Answers

Why is play mode reverting my scriptableobject to a previous serialized state? 2 Answers

Using System.Object in ScriptableObject 1 Answer

Awarding 400 points if we solve this ---- AssetDatabase.LoadAsset("path") loads (null) instead of scriptable object (after enter/exit playmode) 1 Answer

.asset file containing ScriptableObject is empty on openingUnity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges