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 ThePersister · Nov 07, 2016 at 02:39 PM · editorunity5editor-scriptingscriptableobjectreference

Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene?

Hi there, as the title states I'm trying to:

"Include a scriptable object in compiling / not lose it upon compiling without referencing it in the scene."

Here's my situation. I have an EventManager and an AchievementManager Singleton, in the form of Scriptable Objects. I use them in the scene with regular AgEventManager.Instance calls. However, if I do not have the object referenced in the scene, upon loading the project and playing for the first time or when testing a Build, I can't find the singletons.

If I do have them referenced in the scene, everything is fine, it's just that I'd rather not if possible. :)

If you have any idea, please let me know below, it'd be much appreciated!

Looks: imageOfEventManager

Simple reference holder solution (that I don't want): imageOfReferenceHolder

Scriptable Object Singleton Code:

 using UnityEngine;

 // ===================================================================================
 
 public class AgScriptableSingleton<T> : ScriptableObject where T : ScriptableObject
 {
     protected static T m_instance;
 
     // ===============================================================================
 
     public static T Instance
     {
         get
         {
             _fillInstance();
             return m_instance;
         }
     }
 
     // ===============================================================================
 
     private static void _fillInstance()
     {
         if( !m_instance )
         {
             T[] instances = Resources.FindObjectsOfTypeAll<T>();
 
             if( instances.Length > 0 )
             {
                 m_instance = instances[ 0 ];
             }
         }
 
         if( !m_instance )
         {
 #if UNITY_EDITOR
             m_instance = AgScriptableObjectUtility.CreateAsset<T>();
 #else
             Debug.LogWarning( "[AgWarning] Cannot use scriptable objects outside of the editor, so the Instance MUST exist beforehand." );
 #endif
         }
     }
 }

Relative Posts:

http://answers.unity3d.com/questions/156124/are-all-scriptableobject-assets-included-in-the-bu.html

http://ivanozanchetta.com/gamedev/unity3d/unity-serialization-behind-scriptableobject/

https://www.reddit.com/r/Unity3D/comments/35kjr3/where_do_instances_of_scriptable_objects_go/

scriptableobjects.png (46.8 kB)
referenceholder.png (11.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Adam-Mechtley · Nov 07, 2016 at 02:49 PM

Anything included in a folder named Resources is included in your build. Otherwise, anything that is not referenced (even indirectly) by a scene in your build settings will be stripped from the build. You can read more here, but in general use of the Resources folder is discouraged.

I would recommend you continue to include a reference to your singleton (e.g. on some manager GameObject in your scene) and register the singleton in its OnEnable() callback rather than using Resources.FindObjectsOfTypeAll<T>().

 protected virtual void OnEnable()
 {
     // maybe log a warning here if instance is not null
     m_instance = this;
 }


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 ThePersister · Nov 07, 2016 at 03:33 PM 0
Share

The scriptable objects are included in the resources folder. Although when not referenced by the scene, they aren't initially loaded / compiled regardless.

Thank you for the advice though, if no viable solution arises I'll try to avoid the scenario with resources altogether!

avatar image Adam-Mechtley ThePersister · Nov 07, 2016 at 09:52 PM 2
Share

In that case, the standard approach is to use Resources.Load() to find it, rather than Resources.FindObjectsOfTypeAll<T>(), as the latter method only returns objects that have already been loaded (per the documentation), while the former will actually load something at the specified path. That said, using this method is still discouraged as noted above.

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

79 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

Related Questions

Unity inspector scripting - scriptable objects - card game 0 Answers

Confused about custom GameObjects,Custom GameObject confusion 0 Answers

OnInteractivePreviewGUI multiple selection. Works but spams errors. 0 Answers

BCE0044 erro only shows when trying to build .apk 0 Answers

Scriptable Objects with editable functions 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