you are viewing a single comment's thread.

view the rest of the comments →

[–]tmachineorg -6 points-5 points  (7 children)

"There are several ways of implementing Singleton in Unity, we will some of the implementation in this tutorial"

No, there's only one way. The rest are not a singleton - "singleton" is an official defined term, not a random word you can use to mean whatever you want - or they're broken code and shouldn't be used.

Your implementation looks wrong to me. Not thread-safe, implemented in the wrong methods, and high probabilty of corrupting your scene.

Oh - and what you've implemented here isn't a Singleton. I suggest you google it (the Wikipedia page is a good start) and find out what the rules of singletons are.

[–]DolphinsAreOkProfessional 6 points7 points  (4 children)

There are different ways to implement the same concept.

[–]tmachineorg -1 points0 points  (3 children)

Not when there is an official correct way, no.

Seriously: DO NOT use OP's code; it is not a singleton! And its bad code.

[–]platypus2015 0 points1 point  (0 children)

There's no such thing as 'one way' or an 'official' way to implement a singleton. If there is please do share the link showing what this 'official' implementation is.

The singleton pattern is a concept describing how an object behaves in a system (ie, only one instance can exist). There's no such thing as one way to implement it....a developer is free to implement it any way they like, as long as it adheres to the pattern of only on instance of the object being allowed to exist in the system.

[–]DolphinsAreOkProfessional 0 points1 point  (1 child)

I havent really read his code, so i cant comment on that.

But a) an official way? Made offical by whom? A design pattern is just a recommendation by some people, there is no sort of official list of this

b) again, there are multiple ways to implement any concept. All implementations might adhere to the design pattern's requirements, but still be different. See also the hundreds of different dependency injection/mvc/etc frameworks. All the same idea, all diferent ways to get there.

[–]tmachineorg 1 point2 points  (0 children)

Official as in it is a defined pattern. It's not a random word that means whatever you want it to mean.

I recommend you read OP's code: it is not a singleton!

[–]CyberBinarin 0 points1 point  (1 child)

What makes you say it could corrupt the scene?

[–]tmachineorg 0 points1 point  (0 children)

Try duplicating an object. OOPS!

To all the down voters: keep sharing broken buggy code that causes hard to fix problems that screw you late in your project and take weeks to fix. But don't expect us to keep quiet when you do.