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 UnbreakableOne · Feb 04, 2015 at 02:21 PM · cache

Which one is faster? (Cache related)

Hi,

Which one is faster, store a reference to a class or add a static field and make it Singleton?

Thanks

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
0

Answer by Landern · Feb 04, 2015 at 02:29 PM

A reference to an object and a static member are not equatable other than a static class member COULD contain a reference to an object that can be access through that class member that exists once and is accessed through the class, rather than the instance member which is scoped in the scope in which it's instantiated and set on other objects.

The static class member exists across the board. Also a static member is not equatable to a singleton which is a design pattern that reinforces the existence of an instance of an object and access through a static class member...

Your implementation(mileage) will of course be different.

Example:

 // Just a static class member that exists across the board on all Bucket's and references the same memory location, setting anywhere affects all references to it since it only exists as the one and only score member on the class Bucket.
 public class Bucket
 {
     public static int score = 0;
 }

 // usage of just the static
 public int GetScore()
 {
     return Bucket.score;
 }

  
 
 // Singletonish
 public class SingleBucket
 {
     private BucketProperties bucketProps = null;
 
     private SingleBucket()
     { }
     
     public static BucketProperties BucketInstance
     {
         get 
         {
             if ( bucketProps == null)
                 bucketProps = new BucketProperties();
 
             return bucketProps;
         }
     }
 }
 
 public class BucketProperties
 {
     public int score = 0;
 }
 
 // usage of singleton in some method in some class
 public int GetScore
 {
     return SingleBucket.BucketInstance.Score;
 }


Either way you do it, if you're setting references at any point(not the int example but say a concrete type you make) then at some point you will have to get that reference, and that shouldn't different much when it comes to speed. The accessibility is a it different then just a plan static member on a class that has class members and instance members.

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

19 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

Related Questions

Is holding a reference to the Coroutine returned by StartCoroutine necessary? 1 Answer

Can't load objects from asset bundle when using AssetBundle.CreateFromFile 1 Answer

"Caching" code in the Start() to run it "later" 3 Answers

I cannot delete Unity cache 0 Answers

C# - can I cache a GameObject script component in 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