Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MartinMouritzen · Dec 09, 2021 at 01:31 PM · globalmanagementstructure

Objects you use in every scene?

Hi all,

I find myself having certain scripts and objects that I need in every scene. These objects are things like UI components, GameManager scripts etc, and I'm wondering how to do this in the easiest way for myself while developing.

I've read several way forwards, like having a scene that is used for that and doesn't unload. But then it makes debugging hard, because I can't just press play in my normal scenes.

I can also (as I do now) copy them from scene to scene, but this involves manual work, and sometimes I forgot or objects get outdated because I add to them in another scene etc.

So what are the best practices here, and what can I do to speed up development while maintaining sanity? :)

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
Best Answer

Answer by CodesCove · Dec 09, 2021 at 02:50 PM

Few ideas (I'm sure there might be better ones) :

I usually have a ScriptableObject that uses SceneManager.sceneLoaded to call a delegate method whenever new scene is loaded. The "default" scripts I have stored to a prefab gameobject that is instantiated by this method. The Monobehaviours (scripts) in this Game Object are built in a Singleton pattern so they are not accidentally dublicated. They have all static self-reference so they can be easily accessed from other scripts.

I also make some components so that they will be instantiated to scene when ever they are first called. Example for this pattern: MultiTimer:Monobehaviour (Pseudocode)

         private static MultiTimer multiTimer = null;
 
         private void Awake()
         {
             this.CheckMultiTimerAvailability();
         }
 
         private void CheckMultiTimerAvailability() 
         {
             if (multiTimer == null)
             {
                 multiTimer = this;
             }
             else
             {
                 Destroy(this.gameObject);
             }
         }
 
         private static void AddTimerToSceneIfNeccessary()
         {    
             if (multiTimer == null) new GameObject("MultiTimer").AddComponent<CC_MultiTimer>();
         }
 
         public static void StartTimer(float startDelay, float duration, TimerEvent onStartCallback,  TimerEvent onEndCallback, object data = null)
         {
             MultiTimer.AddTimerToSceneIfNeccessary();
             //Do timer stuff
         }

Notice that in above pattern the static self reference is private because we want to call the actual static method (MultiTimer.StartTimer) in this class straight away to support the "first call instantiating". Reason for this pattern is that the Timer uses Coroutines cannot be static and needs to be run in a instantiated Monobehaviour.

Notice also: None of the above patterns have a persistent state over scenes. If you have any need for persistent game data then use ScriptableObjects to store them. You can, of course, make the GameObject persist over sence changes by setting it DontDestroyOnLoad. However I have (nowadays) never really needed to use that since I have used Scriptable objects for game data persistence.

Comment
Add comment · Show 1 · 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 MartinMouritzen · Dec 10, 2021 at 03:26 PM 0
Share

Great idea, which I will certainly use going forward. Thanks a lot for taking the time to answer.

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

131 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

Related Questions

Getting the (instantaneous) normal of a meshfilter object 2 Answers

project wide variables and constants 2 Answers

Global array list c# 1 Answer

Problem with world coordinates in scripts? 1 Answer

Static function and variables error 2 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