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 jfoc2002 · Aug 14, 2020 at 12:25 PM · scripting problemoop

Singelton ScriptableObject or Static class?

I am pretty new to C#...and trying to learn C# and Unity...so i am writing a small game. I learned about the Singleton pattern and all. And made previous games with singleton classes like a "LevelLoader" and a "Music Player". Then i read this article: https://connect.unity.com/p/singleton-scriptableobject

And scriptable objects are also new to me. But i think i get it. In on of my earlier games i had a "GameData" class that implements the singleton pattern. But i remember it was a pain to test in other scenes. So this is a pretty good class to turn into a Singleton Scriptable Object (it just holds values that travels from scene to scene).

However, this class:

 public class LevelLoader : MonoBehaviour
 {
     public static LevelLoader Instance { get; private set; }
 
     private void Awake()
     {
         if (Instance == null)
         {
             Instance = this;
             DontDestroyOnLoad(this);
         }
         else
         {
             gameObject.SetActive(false);
             Destroy(gameObject);
         }
     }
 
     public void LoadSummaryScene()
     {
         SceneManager.LoadScene("SummaryScene");
     }
 
     public int GetActiveSceneIndex()
     {
         return SceneManager.GetActiveScene().buildIndex;
     }
 
     public string GetActiveSceneName()
     {
         return SceneManager.GetActiveScene().name;
     }
 
     public void LoadSceneByIndex(int _index)
     {
         SceneManager.LoadScene(_index);
     }
 
     public void LoadSceneByName(string _name)
     {
         SceneManager.LoadScene(_name);
     }
 
 }

This class is used in several scenes...but it holds no values...so this is better to use it as a static class right? Because there is no values to edit and it needs to be accessed through several scenes.

If it would hold alot of properties it would be better as a Singleton SO?

Am i right?

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
1

Answer by ben-rasooli · Aug 14, 2020 at 12:49 PM

Why do you need your class to be a MonoBehaviour in this case? Just remove that inheritance and you'll be fine using this class from every scene. You also need to change your code a bit:

 public class LevelLoader
 {
     static LevelLoader _instance;
 
     public static LevelLoader Instance
     {
         get
         {
             if (_instance == null)
                 _instance = new LevelLoader();
 
             return _instance;
         }
     }
 
     // the rest of your code
 }

If you need to have some values(properties) with this object AND tweak them in the inspector, then use a Singleton ScriptableObject.

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 jfoc2002 · Aug 14, 2020 at 01:30 PM 0
Share

Now i am confused.

Looks like a singleton pattern on a class....i though they where bad and you should only use it with SO. But since i don´t need objects of it...maybe should be static? Why should i have an object from it (_instance = new LevelLoader())? There is no reason to return an object.

avatar image Pangamini jfoc2002 · Aug 15, 2020 at 11:18 PM 0
Share

The pattern is bad, not the way how it's implemented (singleton, static, singleton SO). I mean there's nothing wrong with using singletons, if done carefully. Unity's API is also quite singleton-based.

avatar image
0

Answer by jfoc2002 · Aug 15, 2020 at 11:04 PM

Anyone else have an opinion about this?

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

240 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

An OS design issue: File types associated with their appropriate programs 1 Answer

Item data structure - Scriptable Object Inheritance problems 1 Answer

Play Animation on Button Press 1 Answer

using a button to stop running script 1 Answer

2 character interaction animation 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