Design Pattern: Singletons in Unity

  Рет қаралды 15,903

samyam

samyam

Күн бұрын

Пікірлер: 56
@samyam
@samyam 4 жыл бұрын
When thanking my new patrons at the end I made a mistake and somehow replaced one of the images I made with the thumbnail 😔 Thank you to Thomasz in the Enthusiastic Tier for your support! ⭐️
@tauheedgamedev2388
@tauheedgamedev2388 4 жыл бұрын
Great tutorial, I'v been using Singletons for a while but with the level of detail in this tutorial I still learned some things. Thanks 👍
@Patricebrouh
@Patricebrouh 4 жыл бұрын
Wow, your explanation is so clear. I can say : "Brackeys has gone, thanks to God we have another Brackeys" .
@samyam
@samyam 4 жыл бұрын
I have a long way to go to get to Brackeys level 😄
@marushirudesu
@marushirudesu 2 жыл бұрын
@@samyam you don't have be brackeys, you have to be samyam that can be better than brackeys.
@chris.davidoff
@chris.davidoff 4 жыл бұрын
This was really informative! I personally do the persistent scene for different things (Like UI, which I may want to unload in cutscenes and such)
@lemetamax
@lemetamax 4 жыл бұрын
This is a really awesome video on singletons, I can't believe I've been using it wrongly. I'll have to watch again to understand it tho.
@leonardo6631
@leonardo6631 4 жыл бұрын
Great video! Thanks for the nice content ❤️
@syngleton
@syngleton 4 жыл бұрын
Sam, this tutorial is incredibly helpful and has completely changed the way I implement this design pattern. The one problem I have with this is I don't know how I'd be able to have a class derive from a base class but also inherit from your Singleton class. The only way I could think of achieving something like this is by turning your Singleton class into an interface, but it doesn't seem like the best approach to me, considering I'd probably have to keep overriding the Awake() and OnDestroy() methods when creating new classes implenting this interface. What would you suggest?
@samyam
@samyam 4 жыл бұрын
Glad you enjoyed it! Hmm maybe you can make a parent empty GameObject as the singleton container for the other game object which derives from that other class? Something like this: Parent.Instance.GetChild()....
@diazpsp
@diazpsp 11 ай бұрын
What is your font in the VSCode? looks nice
@clc4333
@clc4333 4 жыл бұрын
aspiring game dev, just at the very beginning stages... couldn't understand this part, so i'll check out some more easier material then come back!
@SubjektDelta
@SubjektDelta 2 жыл бұрын
If I wanted to have an object in the scene how could I set it to the instance itself? My problem currently is that I have a manager object in the scene and when some other script tries to use the instance a new one is created.
@ericwoodworth8079
@ericwoodworth8079 3 жыл бұрын
ArgumentException: SceneManager.SetActiveScene failed; scene 'Managers' is not loaded and therefore cannot be set active. I've looked over this video many times to make sure I had everything done the same way as you do. My scene is in the build settings properly. Is there something I am missing?
@samyam
@samyam 3 жыл бұрын
Perhaps because the scene is still loading while you are trying to access something. You should wait a frame to make sure it's loaded before accessing the value. You can load it in a coroutine IEnumerator LoadSceneCoroutine(string pSceneName){ yield return SceneManager.LoadSceneAsync(pSceneName); // do something with the scene }
@ericwoodworth8079
@ericwoodworth8079 3 жыл бұрын
@@samyam Awesome! Thanks, I am going to give it a shot
@jorgevalles6835
@jorgevalles6835 Жыл бұрын
In my job I entered in a nightmare due to Singletons. We reached a point in which literally everything you can imagine was a Singleton ☠. So we started to loose control on initialization and closing of several systems, specially when changing scenes. Lots of things crashing because of requests to singletons that will start working in other scene. :(
@danielbezerra5650
@danielbezerra5650 3 жыл бұрын
Thank you 🙏🏿
@lucky4the4turtle
@lucky4the4turtle 4 жыл бұрын
So uuuh, dumb question, but at 4:48 on line 3 what does do wtih the SinglePatternPersistent class? Edit: Nevermind I should have just kept watching the vid.
@ninqbi1738
@ninqbi1738 Жыл бұрын
i dont understand why you put an "inputManager" obj in Managers scene beforehand. Cause as your scripts, it will generate an T object when we firstly start the game. Can u explain it?. Anyway, this is good tutorial, thks
@fernandomilans8444
@fernandomilans8444 4 жыл бұрын
Hi! could you enable the subtitles? thanks!
@samyam
@samyam 4 жыл бұрын
Hi I'm not sure why it wasn't enabled... For some reason it didn't generate the subtitles automatically. I tried re clicking the language but a video online said it might take some time to show up if it's able to generate it. Sorry ☹️
@candle-likeghost9523
@candle-likeghost9523 3 жыл бұрын
Urm.... in 11.03. I need to point out that the "public static T Instance { ...." is not a function but a property. and instead of DontDestroyOnLoad(this), we can do DontDestroyOnLoad(obj), right? (Since Unity destroy the game object with the script when loading, we just need to tell Unity to not destroy the game object when loading).
@samyam
@samyam 3 жыл бұрын
Yes you are correct, however it won't be persistent if no other script calls it's Instance since it will only create the gameobject once it's called. But if you put it in the Awake it is guaranteed to be persistent even if no other script calls it.
@candle-likeghost9523
@candle-likeghost9523 3 жыл бұрын
Oh I see, thanks for explaination
@kendrickjba
@kendrickjba Жыл бұрын
I'm getting this error "ArgumentException: SceneManager.SetActiveScene failed; scene 'Managers' is not loaded and therefore cannot be set active"
@kendrickjba
@kendrickjba Жыл бұрын
I fix it calling the input manager on start instead of calling it on awake on my player script
@zlisarew6830
@zlisarew6830 Жыл бұрын
I have an issue if I go from scene 1 to scene 2 it will load the Managers scene then if I go back from scene 2 to scene 1 it will load another Mangers scene because Awake() in scene 1, I fixed it by checking if (!Managers.isLoaded) then load Managers scene
@samyam
@samyam Жыл бұрын
Because it has a Do not Destroy and it keeps it alive during other scene managers. You need to check if there is already another instance running before starting a new one
@arunachalpradesh399
@arunachalpradesh399 2 жыл бұрын
please reply. how can sound manager be as singleton, as everyone says. because singleton gurantees only one class can acess singleton class at a time. what if sound manager contains gun shot sound, which both player and enemy fire at same time. so how can both players and enemy class access singleton at same time.
@WildPork
@WildPork Жыл бұрын
"because singleton gurantees only one class can acess singleton class at a time" this isn't true unless you have used a "lock" to restrict access.
@youcancallmedoggie
@youcancallmedoggie 3 жыл бұрын
Amazing video! helped me a lot, but i have a question, how to stop persistant scenes loading more then once, like when i restart that scene?
@samyam
@samyam 3 жыл бұрын
The singleton will check if an instance already exists and if so not load the extra object. You can make a manager for that persistent scene a singleton.
@vmmi8215
@vmmi8215 2 жыл бұрын
I've been travelling through some of your video, i can say that you indeed have a really deep understanding about clean & good code, i hope that you would make a video about things to learn for beginner or intermediate to mastery the code principles such as SOLID principle But anyway, i love your lovely voice 😍👍
@samyam
@samyam 2 жыл бұрын
Thank you so much!! I’m hoping to make tons of tutorials more in the future! :)
@playplus5386
@playplus5386 Жыл бұрын
People keep saying that this is a good tutorial, maybe it is, but I'm here to learn the Singleton pattern. However, now I'm confused about the new Unity input system, flags, and many other concepts. Please make a simple scene and focus on the subject: the Singleton pattern. In order to fully understand your tutorials, I have to go back and learn those concepts first, and then come back here. I feel like I'm wasting my time
@LostSol25
@LostSol25 2 жыл бұрын
how long have you been doing game dev? and are you currently working in the industry?
@samyam
@samyam 2 жыл бұрын
Around 6 years! No I'm doing my own stuff :)
@LostSol25
@LostSol25 2 жыл бұрын
@@samyam thats super cool! Thanks and keep going with your tutorials they are amazing!!
@samyam
@samyam 2 жыл бұрын
Thank you! 😄
@SaadTheGlad
@SaadTheGlad 4 жыл бұрын
Hello! Do you know of a simpler tutorial? I'm just trying to get my references to stay correct after switching scenes but I don't know where to start learning... :(
@samyam
@samyam 4 жыл бұрын
Here’s a tut by Infallible Code on Singletons kzbin.info/www/bejne/o6GwYXZsptqEoNU What’s the issue? Make sure you have DoNotDestroy on your singletons so that it persists through your different scenes. However gameobjects in that new scene still need that reference Class.Instance
@SaadTheGlad
@SaadTheGlad 4 жыл бұрын
@@samyam Thank you! I have fixed my issue with the dialogue system (parented all the UI elements and the player to a don'tdestroyonload) but a new problem arose: proper level system. I wanted to make one like undertale but there are no tutorials on the subject...
@samyam
@samyam 4 жыл бұрын
Like leveling up? Code Monkey has an in depth video here kzbin.info/www/bejne/oXymfYCsqMx0iJI
@piggeh6465
@piggeh6465 4 жыл бұрын
Sam can you do animation movement tutorial for isometric? there's like no videos on the subject :(
@samyam
@samyam 4 жыл бұрын
This video might help kzbin.info/www/bejne/qqrapWyqhNqVe7s But I'll put it on my list for future videos!
@piggeh6465
@piggeh6465 4 жыл бұрын
Thanks :)
@CyberAngel67
@CyberAngel67 3 жыл бұрын
I have to ask, why is a singleton hard to test? Proper written Unit Tests can cover a singleton.
@samyam
@samyam 3 жыл бұрын
If your unit test is self contained and you use dependency injection in your code to avoid strongly coupling your singleton instance with a class then it can work fine. But you have to architect your code in a certain way to achieve this, which many game developers generally do not. See: testing.googleblog.com/2008/05/tott-using-dependancy-injection-to.html
@AndyScott67
@AndyScott67 3 жыл бұрын
@@samyam I disagree with that, I have worked in developing Applications with patterns like AOP, DI to name a few. These can all be mocked, and is a major part of doing Unit Tests.
@CCV334
@CCV334 3 жыл бұрын
Why unload the current scene and not just load the new scene. If you load a new scene doesn't it just override the current one. The singleton would also have a DontDestroyOnLoad so why would you need to write anything to load it additively?
@moncicis
@moncicis 2 жыл бұрын
the subtitle is awful
The Power of Scriptable Objects as Middle-Men
17:41
samyam
Рет қаралды 131 М.
The 6 Design Patterns game devs need?
24:20
Jason Weimann (GameDev)
Рет қаралды 374 М.
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 41 МЛН
Empower series #01 - Shaping the future of organizations
12:44
Times of UM6P
Рет қаралды 344
Change Behaviors with the Strategy Pattern - Unity and C#
8:07
One Wheel Studio
Рет қаралды 41 М.
The Ultimate Introduction to Scriptable Objects in Unity
20:45
the comments on my indie game were interesting...
9:00
samyam
Рет қаралды 22 М.
Everything You Need to Know About Singletons in Unity
8:38
Infallible Code
Рет қаралды 89 М.
i made a cozy desktop game in Unity
9:59
samyam
Рет қаралды 9 М.
Events or UnityEvents?????????
15:43
Jason Weimann (GameDev)
Рет қаралды 105 М.
Design Pattern: Object Pooling in Unity
14:10
samyam
Рет қаралды 8 М.
Unity Architecture for Noobs - Game Structure
16:24
Tarodev
Рет қаралды 208 М.