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 tarek-hentati · Jan 02, 2015 at 01:51 PM · singletonstatic-vars

Singleton instance VS static variable ?

In Unity programming is it better to use a static variable or create a singleton instance to manipulate array of objects that will be always in memory and existing?

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

Answer by Fornoreason1000 · Jan 02, 2015 at 01:57 PM

Singleton patterns is used for object types IE i only ever want and need one instance of BattleMananger.

you can't really make a singleton array as far as I know. and can be really annoying to manage. if it wasn't an array I'd definitely say singletones

static variables on the other hand are little more easier but unlike singletons they can easily fall out of control. As for arrays you will not have an issue implementing it.

but singletons are usually better and safer. global variables are known to be "dangerous" while singletons not so much.

if you want you can make a wrapper class for your array and make a singleton of that.

 class MySingleThatHasAnArray : Object {
 
 
 public object[] myArray;
 
  private static MySingleThatHasAnArray _instance;
     private static bool applicationQuit = false;
     public static MySingleThatHasAnArray instance
     {
         get
         {
             if (_instance == null)
             {
 
                 _instance = new MySingleThatHasAnArray();
 
                 //Tell unity not to destroy this object when loading a new scene!
       
             }
 
             return _instance;
         }
     }


 

 





but have a read here. http://www.dotnetperls.com/singleton-static

you array is accesable via MySingleThatHasAnArray.instance.myArray :) for the most part singleton are better

Hope it help

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 tarek-hentati · Jan 02, 2015 at 02:37 PM 0
Share

Thnx for responding Fornoreason1000 but before looking for the singleton implementation, I'm looking for a benchmarking between singleton and static. I must before all make a decision between them :) That's why I should gather the + / - points for each of them.

avatar image Fornoreason1000 · Jan 02, 2015 at 02:38 PM 0
Share

check out the link i posted, it will do a better job than I did.

here what I know there is no"good and bad" it depends on what you want.

-Singletons can have interfaces, static classes cannot
-Singletons can use a factory pattern
-Singletons can have callbacks and Delegates
-Singletons are a single instance of a class, statics a global instance within a class.
-Singletons and statics are accessed globally
-Singletons leave the entire class global while statics only globalize the variables with the static keyword.
-Singletons are created when you decide to, statics are created when you reference them;
-Both are thread safe
-Singleton you can lazy load (load what you need), statics will always load everything
-A singleton is instantiated once, a class with static members can multiple times

avatar image
0

Answer by Owen-Reynolds · Jan 02, 2015 at 05:37 PM

In practice, in Unity it's often easiest to put a monobehave script on an empty with a dontDestroyOnLoad. Which I suppose is technically a singleton.

Being able to see and edit Inspector vars is just so nice.

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 BMayne · Jan 02, 2015 at 05:51 PM 0
Share

A lessor known thing you can do is make your $$anonymous$$anager a ScriptableObject.

avatar image Fornoreason1000 · Jan 03, 2015 at 10:14 AM 0
Share

remember there is a bug that can occur when you have $$anonymous$$onobehaviour singletons. (that is an actual singleton, not like Owen said).

basically you can reference the singleton after it gets destroyed, creating a ghost object, this ghost object is created during he destroy process thus doesn't get destroyed or unloaded. it just sits there. I don't know if its fixed or not, but its better safe than sorry.

but the common fix is to add a Boolean called application closing and set it to true in OnDestroy() then make instance return null when its true. (this maycause a null reference error, but it is to be expected and will only happen when the app closes)

I'm not sure if the same bug applies to ScriptableObjects since im pretty sure they get destroyed in the same process.

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

27 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

Related Questions

Saving last checkpoint hit when reloading scene - Singletons? 1 Answer

singleton, static var, local reference. Whats better? 1 Answer

Create a persistent GameObject using Singleton 3 Answers

Instantiating object from nothing 0 Answers

Class that extends another class that extends Singleton generic class 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