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
1
Question by iamthel0rax · Jul 23, 2014 at 03:19 PM · static variableglobal variable

Static variable for accessing global object?

My project has a lot of objects communicating with a few different global controllers and I've been using this technique to access them:

In the controller:

class Controller : MonoDevelop { public static Controller _;

 void Awake() { 
      _ = this;
 }

}

It seems to work well, and I like the fact that I don't have to have tons of GameObject.Find calls in all of my other scripts. My question is, is this a bad idea? I've never actually seen any code where this is done.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Jul 23, 2014 at 03:21 PM

Yeah it is ok.

Static variables are global in the app. This meaning that after you change the scene its object will be not cleaned up because will be still referenced.

Add this to help unity clean your objects.

 void OnDestroy() {
   _ = null.
 }

The use o FindObjectOfType and cache its value is the unity way of doing this. But no problem.

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

Answer by Paprik · Jul 23, 2014 at 03:35 PM

Static variables may be problematic for automatic serialization (saving & loading). But if you do it yourself or it's a non issue, then I don't see any other problems. It may lead to tighter coupling and more difficult code reusability.

When having more of these global objects, what I do is make only one static, let's call it Game. Then have the other systems as its (non-static) properties. Then you can access them as Game.Player, Game.Controller, Game.AudioManager etc. This avoids the Controller._.var access and also enables you to have more instances, were that ever necessary.

Example:

 public class Game : MonoBehaviour {
     // Singleton instance of this class
     public static Game Instance;
 
     // Non static property
     internal Controller controller;
     public static Controller Controller {
         get {
             return Instance.controller;
         }
     }
 
     void Awake() {
         Instance = this;
     }
 }











Comment
Add comment · Show 1 · 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 Paprik · Jul 23, 2014 at 03:46 PM 0
Share

added an example

avatar image
0

Answer by Paulius-Liekis · Jul 23, 2014 at 03:46 PM

It's called a Singleton pattern (http://www.oodesign.com/singleton-pattern.html), relatively popular thing in OO design. Generally speaking it's ok to use it, because it simplifies access to these objects a lot, although sometimes it can lead to too many hidden relation between parts of the code in the long run. Imagine that your project grew a lot and you use bunch of Singletons around and suddenly realize that one of these objects can not be singleton, but a few objects (with different values) instead. If the project is big, then it might be difficult to refactor, because your code and data everywhere assumes that there is just one object of type X.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Global Variable Error 1 Answer

Static variable reset at LoadLevel 2 Answers

Acessing static variable from Entities.ForEach 1 Answer

Issues sharing variable between 2 scripts 2 Answers

Will static variables not work if negative? 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