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 gregzo · Aug 08, 2012 at 05:56 AM · static

Making a script static - good practise?

Hi to all!

Browsing @Eric5h5 's Vectrosity's code a while back, I noticed the following:

 static var using : TheScript;
 
 function Awake()
 {
    using = this;
 }

I've been using that trick more and more, so much so that if I know a script will only have one instance, I'll automaticaly make it static. Avoids a lot of code maintenance, and is plain handy, but does it impact performance? Is it bad practise?

I use it a lot to reference stuff too, like this:

 //MasterScript
 static var using : MasterScript;
 var slaves : Slave[]; //populate in inspector or in Awake(GetComponentInChildren)
 
 function Awake()
 {
    using = this;
 }

Any script can then call MasterScript.using.slaves[slaveNb].DoWhatever();

I'd very much appreciate informed insights!

Cheers,

Gregzo

Comment
Add comment · Show 2
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 asafsitner · Aug 08, 2012 at 07:15 AM 1
Share

Static classes are great, very useful. I use them around all the time, and haven't yet encountered any problems.

avatar image Kryptos · Aug 08, 2012 at 07:40 AM 0
Share

@asafsitner your comment is misleading. You make it look like it is a great idea with no drawbacks. Unfortunately this is not the case (see my answer).

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Kryptos · Aug 08, 2012 at 07:34 AM

Static variables are bad practice. Use them sparingly. Not because they are really bad for performance (the GC collection does work differently though), but because most users misuse them.

My advice: only use one static variable for your singleton object, then put all other global variables in this singleton.


Example of typical misuse: referencing an object that is not persistent. Part of the object will be destroy automatically by Unity (i.e. Unity will forget about it) when loading a new scene. But most memory will not be release due to this last reference (and secondary references and cross-references), hence a memory leak.

Possible workaround: wrapper method for loading a level that takes care of releasing references + reconstruction of the references upon level loading.

And to prevent headaches asking myself whether this script or this other script should or not release references, I tend to generalize releasing references in all my script:

 public class MyClass : MonoBehaviour
 {
     MyOtherClass someRefObject = null;

     void Start()
     {
         // Get a reference to the MyOtherClass object, for example with FindObjectOfType
         this.someRefObject = (MyOtherClass) FindObjectOfType(typeof(MyOtherClass));
         
     }

     void OnDestroy()
     {
         // Release reference upon destruction
         this.someRefObject = null;
     }
 }
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 gregzo · Aug 08, 2012 at 08:33 AM 0
Share

Thanks for the detailed reply,+1. I understand your concerns, but what if you know for sure refed objects will not be destroyed? Your solution would complicate things needlessly, no? $$anonymous$$y current project is for mobile, so I'm avoiding Instantiate and Destroy as much as possible, recycling ins$$anonymous$$d.

avatar image Kryptos · Aug 08, 2012 at 08:35 AM 0
Share

If you only have one level, then it is ok. But once you load a new level you will have memory leaks if you're not careful about it. And for iOS, lack of memory can crash the game.

And in my experience, you never "know for sure". In fact you should never think that way. It will prevent you from discovering very annoying bugs at the end of the development.

avatar image gregzo · Aug 08, 2012 at 08:45 AM 1
Share

I'm not sure I understand if my use case would cause leaks or not... Example : scene0:menu, scene1 : on screen keyboard. $$anonymous$$eyboardScript has "static var using = this" and references 49 $$anonymous$$eyScripts in an array(not static). Back to main menu, $$anonymous$$eyboardScript and all keys are destroyed. Where's the leak?

avatar image
1

Answer by Ludeme Games · Aug 08, 2012 at 07:33 AM

Yes, I think this can be quite good... but every technique is only good to solve certain types of problems. Do you want a kind of manager class for something you know there will only be one of? For example, a SoundManager? In that case this can be much faster than finding an object with the component in the hierarchy or more memory efficient then storing a reference to the found object where it is used.

To extend the idea further, you may want to warn if using != null before it's assigned... to make sure that the pattern is used properly. You may also need to be aware if objects are destroyed in Slave[].

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 gregzo · Aug 08, 2012 at 08:39 AM 1
Share

I'm using this technique to ref objects of finite number, example: all keys of a keyboard. $$anonymous$$eys.using.keys[noteNb].DoWhatever. Will investigate what $$anonymous$$ryptos says, but I can't really see so far how my use would cause leaks...

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

10 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

Related Questions

Unknown identifier, OnCollisionEnter Error 1 Answer

Global Varible Problem 2 Answers

"transform" in a static function 1 Answer

Can I run static var from another script ؟؟؟ 2 Answers

Changing static variables from another script? 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