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 Elenesski · Jan 19, 2014 at 07:42 AM · staticinitializationglobalvariables

Is it possible to control the order of a static's initialization?

I am aware that in order to create the idea of global variables, you need to use static ... but I'd use static properties, not variables so I can capture the change events. What I was planning on doing is having a dummy object in the corner of my scene that contained all of my statics.

The question is, when the scene is loading, is there any way to force the dummy object to always load first? Is there a declarative I could use, or perhaps the object name that guarantees it's first?

Comment
Add comment · Show 4
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 Benproductions1 · Jan 19, 2014 at 12:59 PM 0
Share

What are you trying to achieve? static properties don't get "initialized" in any way you can cause side effects so I don't see how those are relevant in any way.

$$anonymous$$aybe you can give an example to make it more clear?

avatar image Elenesski · Jan 22, 2014 at 06:36 AM 0
Share

I am trying to initialize an interface to a database. So I have to open the database via a connection string, set up some parameters, initialize some of my parameters etc. Other statics will assume the database is loaded. This is part of the initialization of the game.

Or should I just implement some kind of lazy load and let the first call do the initialization?

avatar image HappyMoo · Jan 22, 2014 at 01:09 PM 0
Share

Yes, DB always lazy. Also, this sounds like you're trying to keep the database connection open, which usually is a bad idea. You should just open a conenction if needed and then close as fast as posibble when the work is done. Database connection pooling is usually implemented a level above your application layer, e.g. by ADO.NET. And if you need to do extra work the first time, just set a flag and check it in the central method that returns a connection or somwhere similar.

avatar image Elenesski · Jan 22, 2014 at 08:07 PM 0
Share

I'll implement it without keeping the connection open. Thanks.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by HappyMoo · Jan 19, 2014 at 01:05 PM

You don't need a dummy object.. statics work without objects... that's the idea. Just create your static class and that's it. You can access it everywhere... no need for a dummy object.

Comment
Add comment · Show 3 · 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 Elenesski · Jan 21, 2014 at 06:28 PM 0
Share

Fine, but how do I make sure those statics in that class are executed first before any other statics in any other class?

avatar image HappyMoo · Jan 21, 2014 at 08:53 PM 0
Share

What are you trying to do? I have the feeling you're trying something that can be solved easier in another way. All static members get lazy-initialized before you try to access any static member or before any object of that class is instantiated. So, nothing is happening before you try to access the static members for the first time anyway.

avatar image Benproductions1 · Jan 22, 2014 at 02:04 AM 0
Share

Read this

avatar image
1

Answer by jaroosh · Jan 22, 2014 at 08:40 PM

how statics work in .Net and I'm assuming youre using c# scripts is they will get defined (initialized) the first time they're used so you dont have to worry about initialization. However, if you're using statics for something more than just constant values (consts are also static internally), using statics is not the best choice imo as it makes the code less testable and hard to maintain when the project gets big so I'd say its way better to use a Singleton pattern and define those global variable scripts as singletons, add them to an empty object in the scene and use script execution order (http://docs.unity3d.com/Documentation/Components/class-ScriptExecution.html) to ensure that your variables are initializes when they need to

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

21 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

Related Questions

Static Initialization 1 Answer

Initialising List array for use in a custom Editor 1 Answer

class constructor/order/initialization confusion 1 Answer

What the best way to initialize constant static data in a MonoBehavior? 0 Answers

How to access script from another script in an object? 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