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
0
Question by jldooley1 · Jan 11, 2021 at 05:10 PM · static

Statics Best Practice

I'm setting up the management system for a game project I'm working on, which at its core has a GameManager class which carries across all scenes via singleton. Within each scene is a LevelManager class which holds properties specific to that level (player start position, etc.). A reference to the current LevelManager is added to the GameManager when a scene launches, and is set to null when the scene changes. I'm currently assigning the LevelManagers to a Static property, but I'm wondering if there is any point to doing so if I'm going to reference the GameManager instance anyway. Do people have an opinion on whether it is better to keep it as a static or just use a public property?

  public class GameController : MonoBehaviour
     {
         public string helloWorld = "Hello World";
     
         private static GameController _instance;
         public static GameController Instance
         {
             get
             {
                 return _instance;
             }
         }
     
         private static LevelController _currentLevelController;
         public static LevelController currentLevelController
         {
             get
             {
                 return _currentLevelController;
             }
         }
     
         void Awake()
         {
             if (_instance == null)
             {
                 _instance = this;
                 Debug.LogWarning("GameController assigned on awake.");
             }
         }
     
         public void AssignLevelController(LevelController levelControllerInstance)
         {
             Debug.Log("LevelController unassigned: " + currentLevelController);
             _currentLevelController = levelControllerInstance;
             Debug.Log("LevelController assigned: " + currentLevelController);
         }
     }
     
     
     public class LevelController : MonoBehaviour
     {
         private GameController gameController;
         public string property = "Property Recieved!";
     
         void Awake()
         {
             gameController = GameController.Instance;
             gameController.AssignLevelController(this);
         }
     }
     
     
     public class GetLevelControllerProperties : MonoBehaviour
     {
         private LevelController levelController;
     
         void Start()
         {
             levelController = GameController.currentLevelController;
             StartCoroutine(GetProperty());
         }
     
        IEnumerator GetProperty()
         {
             yield return new WaitForSeconds(5f);
     
             Debug.Log(GameController.Instance.helloWorld);
     
             Debug.Log(levelController.property);    //Has to access through the GameController
         }
     }
 
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 logicandchaos · Jan 12, 2021 at 10:09 PM

Don;t use singletons! They are the DEVIL! :P Unity's scriptable objects make singletons unnecessary, the pattern itself it not too bad, but how they are abused. You have a GameManager singleton that has don't destroy on load and I bet tons of unrelated data, just used to save across scenes. ScriptableObjects don't live in scenes so you could use one as a GameManager, but that is still a big monolithic class, which is really the root to all the problems. You need to use scriptable object architecture, make scriptable object variables that you can plug in anywhere. https://www.youtube.com/watch?v=raQ3iHhE_Kk&t=1051s

Comment
Add comment · Show 6 · 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 MSavioti · Jan 13, 2021 at 12:49 AM 0
Share

I usually don't $$anonymous$$d recommending these "bad" practices like Singletons when I the one in question isn't yet very familiar with code, because it would be too much for they to handle anyway.

avatar image logicandchaos MSavioti · Jan 13, 2021 at 12:55 AM 0
Share

I don't think so learning it the SO way 1st may make things easier, I wish I knew about SOs when I 1st started with unity, I would be so much further long right now. I did forget that sometimes a single can be utilized, but they should only do a single purpose, code architecture is really important and I wish I learned more of it from the start, it's harder when you have to unlearn all the bad practices.

avatar image MSavioti logicandchaos · Jan 13, 2021 at 01:05 AM 0
Share

You definitely have a point but I don't try to enforce "best" practices too much on beginners because it's so easy to give up when you're just learning the basics. Even having a little coding background when I started I struggled big time in the first months.

avatar image Bunny83 · Jan 13, 2021 at 03:01 AM 1
Share

I always recommend watching the talk from this point on. It's just too good to be skipped ;)

avatar image jldooley1 · Jan 20, 2021 at 03:06 PM 1
Share

Thanks, I watched the video, then watched it again to take notes! Our lectures only went as far as making us aware that scriptable objects existed but it's great to have actual use cases demonstrated. Kind of wish I'd know about it a few months ago, would have saved me a lot of time.

avatar image logicandchaos jldooley1 · Jan 20, 2021 at 03:11 PM 0
Share

Ya I learned about them so late and they are so useful!

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

114 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

Related Questions

how to make easily accessible classes. the type like Unity has in JS for example. 1 Answer

Static IEnumerator!? 1 Answer

C# Going Static or Going OOP 3 Answers

trying to delay a loop using yield - not working 1 Answer

How do we access static prefabs? 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