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 nuverian · Aug 25, 2012 at 12:36 PM · javascriptsingletondesign-patterns

singleton, static var, local reference. Whats better?

Hi I've spend last day researching on about this, but still have a question in regards to how to set up GameManagers.

I can probably understand why one would use a singleton over static variables, and if I get it correctly is the the very fact to avoid static variables themselves. On the other hand, I presonaly find it easier to read code when I make a private local variables on each script I want to read from the GameManager, so that later on in the script I code like for example m_playerScore = 5; instead of GameManager.instance.playerScore = 5;

Or for functions I prefer coding: m_gameManager.DoSomething(); instead of GameManager.instance.DoSomething();

Though obviously the fact that I prefer it doesn't mean anything if this is a bad practise. So my quetion here is, why not do this instead of having a singleton for referencing to a GameManager, SpawnManager, InventoryManager etc etc from other scipts that need them?

I've done my research and all but it didnt answered me this. Thanks

Edit--Javascript

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 ScroodgeM · Aug 25, 2012 at 01:41 PM

the main reason to use singletons instead of totally static class with static variables is cause unity serialize only instances and it's public properties that are marked as serializable. so, if your class should have one instance in project and needn't to store any external data/links using unity serializer, you can use static class

Comment
Add comment · Show 4 · 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 nuverian · Aug 25, 2012 at 02:30 PM 0
Share

Hi thanks for your answer. But mostly my question was not about why use Singleton over Static variables, but rather if it's bad for some reason to use local references ins$$anonymous$$d of either singleton or static .Thanks

avatar image ScroodgeM · Aug 25, 2012 at 09:23 PM 0
Share

i don't know reason why it's bad to use local references. simply be aware with it, cause unity sometimes use structs that can't be referenced.

in unity optimization guide there are recommendations to use local references once linked at start for fast future use

http://docs.unity3d.com/Documentation/ScriptReference/index.Performance_Optimization.html

avatar image nuverian · Aug 26, 2012 at 09:12 PM 0
Share

Thanks. for the link. Call me a noob or something (i am) i still don't know how to handle this :/ damn.

avatar image ScroodgeM · Aug 27, 2012 at 07:11 AM 0
Share

what exactly you can't handle? you can attach scripts to other scripts (instances-to-instances) for fast use in future, you can find existing by type or object name a store reference to script etc.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity3d Javascript Reference Script Variable 1 Answer

I have a movement script, but how can i make it so i can move half the speed while in the air. 0 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