0

I have a couple small questions about ScriptableObjects.

1) From doing menial tests, it appears that the scriptableobject asset is never loaded into memory (i.e. deserialized) unless there is an object that is already referencing it. Is there a way that I can make these scriptableobjects load at the start of the game without any hard references to them?

2) I am trying to use hideflags.DontDestroyOnLoad to keep the scriptableobjects alive throughout the game. I read on the documentation that you need to destroyimmediate these objects to prevent memory leaks. Do i need to add that to OnDisable or something similar to kill itself when done? I intend to use the same scriptableobjects again when the game is reopened, i just wasn't sure if it was entirely necessary to kill the objects every time the game is closed.

Thanks, I am trying to grasp exactly how scriptableobjects function and some best practices when using them.

2
  • How do you want to use the ScriptableObjects? How can you use a ScriptableObject created at edit time if you don't have any direct reference in it from one of your scripts? Commented Sep 10, 2018 at 21:29
  • Well, i was hoping to make like a scriptableobject utility that holds all the references to the scriptableobjects and manipulate/access them via static methods. So i guess technically all the individual scriptableobjects would be referenced by the utility but nothing is referencing the utility itself, so the utility doesn't get loaded/deserialized. Also, any insight on the dontdestroyonloads? Commented Sep 10, 2018 at 22:34

1 Answer 1

2
  1. As @Hellium noted in the comments, if nothing is referencing them then nothing can be using them. If your manager/utility holds references to all of them then you just need to load it. A common way to do so is to just use Resources.Load with a hard coded path to the utility's prefab/ScriptableObject.
  2. Unity destroys all scene objects when you load a scene. DontDestroyOnLoad stops it from destroying a particular object. ScriptableObjects are not scene objects so they are unaffected by either of those things.

I read on the documentation that you need to destroyimmediate these objects to prevent memory leaks. Do i need to add that to OnDisable or something similar to kill itself when done?

No, and Unity will actually complain if you try to do so. ScriptableObject.OnDisable is only ever called when it is being destroyed by the engine. It's not like a MonoBehaviour where you can enable and disable it at will.

I intend to use the same scriptableobjects again when the game is reopened, i just wasn't sure if it was entirely necessary to kill the objects every time the game is closed.

When you load the game again the utility/manager will load up the same references as the first time.

I'm unsure from the way you phrased it, but some people see the way it works in the editor (Unity undoes all changes to scene objects when you leave play mode, but ScriptableObjects are usually assets which are unaffected, so their data will persist) and they mistakenly think that it works the same in a build. It does not. When you build, the values of all your prefabs/ScriptableObjects/other assets are all fixed in. Each time you run that build you will start with the same values as when you built, regardless of any modifications you made last time you ran the game. You can't just use ScriptableObjects as an easy way to save stuff at runtime (it would be awesome if you could though).

Sign up to request clarification or add additional context in comments.

2 Comments

Rats! I watched this lecture youtube.com/watch?v=raQ3iHhE_Kk about scriptableobjects and what i thought he was saying was that the assets get serialized and stored physically in memory so that changes i made could persist between plays. So that doesn't work on builds, only in the unity editor? On second look deeper into the topic, i just now learned that the assets are almost like a read only chunk of data on builds. Any advice on how you would go about saving scriptableobject data between plays?
They're not at all readonly, you can change their values all you want, they just won't remember those changes after the game is closed. ScriptableObjects don't help you at all if you're trying to save data in a build. You have two separate types of data: SOs hold stuff you want to set in the editor and fix into the build, and most of the time you use a separate serialization system like Json or ProtoBuf for stuff you want to save at runtime. I haven't yet had a situation where I needed to set stuff in the editor and then change and save it at runtime so I don't have a solution for that problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.