Lazy Initialization vs Base Singleton

Thanks for providing this package!

I was just curious how this functionality compares to the Base Singleton class used in the 2d RPG course? If I already have the singleton system from that course, will it accomplish the same functionality as LazyValue?

Per my understanding, the purpose of both of these classes it to be able to initialize and maintain a reference to a specific value or component?

LazyValue and singletons are two very different things.
The singleton holds and maintains a single instance of an object. Any access to that object will always result in the same object being accessed. That is the purpose of a singleton.
The LazyValue we use only defers initialisation until first use, but it does not ensure that there is only one instance of the object. We can have many objects that are ‘lazy’.
If you were to ‘singleton’-ify a Health component, any attempts to use that component will always give you the same component back. Even if you instantiate a new copy, the singleton pattern we use in Unity will just destroy it again (well, there are different flavours, but the one I use will). If you LazyValue-ed a value multiple times, you will have multiple instances, each holding its respective value.
I hope this makes sense.

3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms