Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by RynthGames · Jul 17, 2020 at 09:44 PM · scriptableobjectscene-switchingsingleton

Persistence of ScriptableObjects between Scenes?

I've recently been looking at scriptable objects as a new tool to build my game, and I've recreated a set of classes that can hold variables and references for different data types that can be dropped into and shared between objects in my scene as required. Now, I'm aware that if I alter a scriptable object at runtime whilst I run it in the editor, it will actually save into the .asset file, meaning that it would also persist across scenes. I'm also aware that it will not do this at runtime in the build as it cannot serialise the data back into the .asset file outside of the editor. My question is, if I reference the .asset file in 2 different scenes and change it in one scene, will it keep the value when I load the other scene (i.e. will it keep the value in memory for the duration of the session within the build, or will it reload it from the .asset file when I switch scenes)?

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
2

Answer by Elango · Jul 18, 2020 at 05:21 AM

Changed values will stay for a whole application runtime as long as you not null all references in current scene and unload SO with

 Resources.UnloadUnusedAssets();

or

 Resources.UnloadAsset(so);

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 RynthGames · Jul 18, 2020 at 10:47 PM 0
Share

Thanks for your response on this! Just a couple more questions in regards to the answer you've given. Firstly, am I correct that SOs may not persist if I were to load an intermediate scene that doesn't have any references to them? Secondly, does Unity call Resources.UnloadUnusedAssets() itself at any point (I'm guessing it may call it once a scene has been loaded to clean up any objects that are hanging around, which leads me to the assumption in the former question)?

avatar image Elango RynthGames · Jul 19, 2020 at 06:03 AM 0
Share

Loading new scene (not additive) will unload unused resources. So if there's no direct reference to SO in new scene (in inspector) it will be unloaded (assigning it in Awake/Start doesn't count as direct). If you store reference in static class variable it will stay. As far as i know Resources.UnloadUnusedAssets() called on enter playmode, loading scene (inc opening new scene in editor) and when memory limit reached.

avatar image
1

Answer by RedHillbilly · Jul 29, 2021 at 06:00 PM

Answering here because it's the first hit on google. Found the solution to not unload scriptable objects between scenes, even if they are not referenced in one scene (http://answers.unity.com/comments/1569389/view.html ). Add the following snippet of code to you SO:

 private void OnEnable() => hideFlags = HideFlags.DontUnloadUnusedAsset;
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.

Follow this Question

Answers Answers and Comments

137 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

Related Questions

Unity : Singleton ScriptableObjects resets after play 1 Answer

Timer Data Across Scene Changes 1 Answer

Single Scriptable Object Asset 1 Answer

How do I get a U.I. image to remember if it is active or Not Active Across Scene Changes Using Arduino 2 Answers

resources loaded in one scene not transfered to other 0 Answers


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