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 Jessespike · Aug 01, 2012 at 07:31 AM · memorystaticinstancesingletonleak

Would setting this static cause a memory leak?

My friend's script has a static instance to itself. We had a debate and I wasn't sure later. My question: Would setting this static in a Awake/Start cause a memory leak when the scene gets reloaded?

 public class ExClass : MonoBehaviour {
 
     private static ExClass instance;
 
     public static ExClass Instance {
         get { return instance; }
     }
 
     private void Awake() { 
        instance = this;
     }
 
     //...
 }
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
2
Best Answer

Answer by CHPedersen · Aug 01, 2012 at 11:36 AM

No. :) If the scene gets reloaded, then a new instantiation of this script will be made when a fresh copy of whatever object it is attached to is loaded during scene load. This causes the Awake method to fire again, but this doesn't create another copy of the static ExClass reference, only a fresh copy of the actual MonoBehavior it points to. The old copy that the static variable pointed to was destroyed along with the object it was attached to when Unity left that scene upon reloading.

In summary: What you're seeing above is just a way of making a script reference statically available so you can access it through ExClass.instance instead of having to go through GameObject.Find([Gameobject_name]).GetComponent() to get a reference to it. This is generally fine, but only if this particular script is attached to only one gameobject and only once to that gameobject.. If not, the static reference gets set to whatever instance of this script happens to load last, which is arbitrary.

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 CHPedersen · Aug 01, 2012 at 11:41 AM 0
Share

I really hate that Unity Answers tries to avoid script injection by disallowing the greater-than / less-than characters. Does anyone know how to get around that so I can properly denote use of generics?

avatar image Jessespike · Aug 01, 2012 at 01:30 PM 0
Share

Oh I see. Thanks for the clear answer, it makes sense now.

avatar image CHPedersen · Aug 01, 2012 at 01:46 PM 0
Share

Glad it made sense. :) Could I get you to please mark the answer accepted?

avatar image sdgd · Jan 28, 2013 at 05:09 AM 0
Share

yeah I've figured that out

< space "stuff you need to write" space >

if you do without spaces it eats what ever is between this 2

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

Avoid NullReferenceException with Singleton 3 Answers

Singleton instance accessed in coroutine always null 1 Answer

Create an Instance? 2 Answers

Do I need to manually destroy material/model instances that are lazily copied? 2 Answers

Best practices for garbage collection? 3 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