How to use Singletons in Unity3D without breaking everything...

  Рет қаралды 52,238

Jason Weimann

Jason Weimann

Күн бұрын

Пікірлер: 125
@diliupg
@diliupg 5 жыл бұрын
Jason you must use a compressor on your microphone to keep the audio level consistent, and an equalizer to reduce some frequencies around 3000 - 5000hz.
@gregoryfenn1462
@gregoryfenn1462 5 жыл бұрын
The audio has a bit of feedback and jumps, are you using a new microphone or sound software? Something isn't quite right!
@Unity3dCollege
@Unity3dCollege 5 жыл бұрын
yea something went terribly wrong w/ the audio. It'll be fixed in the next one, sorry about that :(
@nikolazagorac8634
@nikolazagorac8634 Жыл бұрын
There is a workaround for singletons, but if you are working on a team you must communicate how the system works. What you do is make your singleton base class abstract, add a protected abstract function called Initialize or OnAwake or whatever. Now also in the base Singleton class in the Awake function, after all the base class related code just call the Initialize function. So now all the child classes of the Singleton class require you to implement the Initialize function which is automatically called on the Awake function from your base class. Just make sure to never use the Awake function in your child classes. One step further would be to have a static function in your base class with the RuntimeInitializeOnLoadMethodAttribute attribute to automatically create an instance of your class when the scene loads.
@AnthelmeDumont
@AnthelmeDumont 5 жыл бұрын
I really want to thanks you, every time I watch one of your videos I learn something new and I think it helps me to become a better developer. Keep the good work Jason.
@giggleghost3751
@giggleghost3751 5 жыл бұрын
I love your content Jason, you are senior developer that this community needs to learn some good coding practices! I think it's about time you invest into a proper mic. Spend $100 - $150 on it. Get Blue Yeti or Samson G-Track Pro, both excellent USB mics. Your channel deserves good audio!
@tarzanzabujca
@tarzanzabujca 3 жыл бұрын
So the main two points i got from it: 1. Singleton makes stuff easier than: GameObject.FindObjectOfType() on every manager my game has, instead i have just static instance of one object, and i can event prevent duplicates :D 2. static field values on MonoBehaviour are the same for ALL monobehaviours of same type, you dont have to cycle through every one to set one specific global value. hmm TIL :D TYVM for you hard work Jason! :D
@shannonrowe9402
@shannonrowe9402 5 жыл бұрын
Good overview of the subject. I make quite a bit of use of singletons that involve double locking and a few other tricks to mitigate race conditions. In case it helps anyone, here's an extract that shows double lock usage and validation checks, with some comments: public class ManagerTemplate : MonoBehaviour where T : MonoBehaviour { private static object lockObject = new object (); private static volatile T instance; public static T Instance { get { if (instance != null) { return instance; } // Safeguard against calls after destroy has begun if (lockObject == null) { return null; } // Try to take out a lock lock (lockObject) { // Once we have the lock, do a second check to make sure another thread didn't get there first if (instance != null) { return instance; } // Fetch the instance of the class if already in the scene // Note: there should only be one copy of the singleton attached to a GameObject in the scene T [] instances = FindObjectsOfType (); // For this project we are not interested in spawning new objects to hold these if they aren't found, as if // it's still not already in the scene, then we have a problem of quality control anyway if (instances == null) { Debug.LogError ($"{typeof (T).Name} not present in scene"); } else if (instances.Length != 1) { Debug.LogError ($"{instances.Length} instances of {typeof (T).Name} present in scene"); } else { // Correctly found the only instance, so assign it instance = instances [0]; } } // Return the existing or just identified instance return instance; } } }
@abwuds7208
@abwuds7208 5 жыл бұрын
Great video Thank you! Please do a tutorial explaining the differences between caching components in OnEnable, Starts or Awake and the best practices around these methods... It would be so cool!
@Xankill3r
@Xankill3r 5 жыл бұрын
A good pattern to use in place of SIngletons is the Service Locator. 1. Define an abstract class deriving from Monobehaviour that defines the public interface to your Service 2. Create Service(s) deriving from your Abstract Base 3. You can have premade GameObjects in scenes that have your derived Service(s) attached as components 4. Lazy instantiation searches for the Abstract Base using FindObjectOfType 5. That's it, you're done This allows you to test multiple implementations based on the same interface enabling easy testing as you can create mocks very quickly. Bonus: Mark the derived classes as Obsolete with the flag to throw compiler errors. Although VERY hacky this allows you to easily enforce good programming practices as the option for some shortcuts is removed. It should force programmers on the team to think more explicitly about the interface of their classes and to utilise proper access modifiers.
@broganking9830
@broganking9830 5 жыл бұрын
Loving the use of Rider! Best IDE for Unity by a million kilometers
@justinwhite2725
@justinwhite2725 5 жыл бұрын
For something like an audio manager you'd want to set it to dontdestroyonload. This will keep it persistent even if you change scenes.
@YiffyStiffyMeow
@YiffyStiffyMeow Жыл бұрын
I don't think this applies to static/singleton classes? You can't instantiate objects from a static class therefore there's no need to destroy anything
@worldeater08
@worldeater08 5 жыл бұрын
I would love a video about data oriented design in unity
@cristiancaze
@cristiancaze 5 жыл бұрын
ohh that book of lenses.. veryy goooood :)
@paulroos8517
@paulroos8517 5 жыл бұрын
It might be, that the distance to your microphone varies as the volume varies. Keep in mind, that the human ear adapts to the volume level. So, when your voice goes from loud to soft, one misses a few soft words, following the loud voice. Fortunately, there is the transcript.
@pirateskeleton7828
@pirateskeleton7828 2 жыл бұрын
I use a static class called Conventions to hold all of the useful functions that I tend to use throughout my different objects.
@clamum
@clamum 4 жыл бұрын
Thanks for the video, Jason. For the AutoCleanupSingleton, on line 27, why do you call "Destroy(this.gameObject)" rather than "Destroy(_instance)"? Thanks
@rill3
@rill3 3 жыл бұрын
Saw this comment and I know it's old. But I wanna answer it anyway as someone else might have the question. He is destroying the "new" gameObject that was just created. The instance has the old gameobject that is the "true" source. So the new object is created and say "Hey, there is already someone here. Talk to this guy called instance instead and I'm gonna leave". Something like that.
@dand4604
@dand4604 5 жыл бұрын
Hello sir, I'd like to ask you to please either answer me in a comment or make a video for people with the same question. How do you organize everything, how to think and avoid getting overwhelmed by tasks and information and how to stay focused. I find this to be a very important yet difficult task especially for beginner programmers.
@mehmedcavas3069
@mehmedcavas3069 5 жыл бұрын
Hey jason again a very nice topic and thanks a lot for the tutorials :) They help me a lot on my internship :) Want to ask u anything. What do u think about the scriptable object pattern, using them instead of singletons?
@rickloyd8208
@rickloyd8208 5 жыл бұрын
In addition to your second implementation, I also like to add donotdestroy on a singleton object, that way I can preserve data which was calculated and loaded from internet in previous scene. As for the issue regarding awake, I solved it with virtual keyword (if I recall). The only problem which remains - no way to know the order in which singleton will be called, so there will be cases when some objects are not created yet while you are trying to access them. So I want give a try to avoid using them in next project.
@trashcaster
@trashcaster 2 жыл бұрын
You can likely call base.Awake() in any implementations too. That's how you're meant to call parent-class methods.
@i_am_reshad
@i_am_reshad 2 жыл бұрын
Here is a problem I came across when using singleton pattern on my prefab. I have 3 box (same prefab). each of them has one same script called changeColor (because it is prefab) ; I use singleton in this class like if(isntance == null) { instance = this;} else{ destroy(gameObject)} . When game stars all of other boxes ( 2 boxes of 3 ) are disapppearing. I found the problem is when Instance in not null it destroys other boxes and keep only one box. What should I do here.?
@niravsenjaliya1206
@niravsenjaliya1206 5 жыл бұрын
u r awesome.plz keep sharing this kind of advance things. have a good day!!!
@kutanarcanakgul5533
@kutanarcanakgul5533 5 жыл бұрын
I want to ask something. You always say you should separate your code for the future big things. Let's say i have a player and it has movement system,input system,inventory system,health system etc... I want to separate all of that but Player has a super(base) class called Character. How should i implement all of that i cannot figure that out. Is there a video out there you explain kinda stuff?
@trashcaster
@trashcaster 2 жыл бұрын
You should have separate classes for your Inventory, for an Attribute system (HP, strength, dexterity, luck, etc). Any object type that you want to be able to use that, create a class that can be used generically, and make Player and Monster/NPC/whatever implement it. This way you can find any kind of "entity" to modify values, without having to hardcode each type with if statements. Another option is to have a sort of blank-slate class that contains all the inventory, stats, etc and has a Scriptable Object that defines the type of entity. Such as making an EntityScriptableObject class with a string field set called type, and set the value to "Player", max HP set to 20, inventory size to 40, etc. Define non-dynamic properties in the scriptable object instances you make in the project, and then when you create a new Entity game object, just assign the scriptable object that corresponds to the type. This way also lets you create new types of enemies by using drag and drop + editor, and as long as you create a really generic framework to handle it, it can do anything you need it to. You can assign meshes, prefab references, numerical values, strings, lists/arrays, etc. Just remember that this approach is only for making "templates" that your game handles.
@paulrobinson4932
@paulrobinson4932 5 жыл бұрын
Hi Jason, any chance you can do a tutorial covering spawning enemies in a 2D game with trigger. I'm making a 2D game and have a script that lets me add enemies and spawnpoint in the inspector but struggling to get the whole thing to work, please help. Love your other tutorials :-)
@ElegantWaster
@ElegantWaster 5 жыл бұрын
The audio issues make the video difficult to listen to
@Shurid16
@Shurid16 5 жыл бұрын
Can u fix the audio please? The pitch is so high.. Hurts my ears
@zekiozdemir420
@zekiozdemir420 2 жыл бұрын
thank you
@user-lv6qm3fj2z
@user-lv6qm3fj2z 5 жыл бұрын
One useful addition. Avoid at any cost using singletons when you apply for the job in the company and do the test task for them. There are a lot of really weird people out there, who will reject you just by seeing this pattern in your code. Happened to me a lot of times.
@nowayjosedaniel
@nowayjosedaniel 3 жыл бұрын
This actually makes a lot of sense. Many bad programmers (and even some good ones) fear valid tools because they abide by weird religious dogma. As strange as it is, your advice is good. It's the same advice as avoiding talking about literal religion or politics in an interview or at all in most cases. You never know what team they mindlessly follow or will find offense with. Tribalism sucks but it's too real to just ignore, and for some weird reason some people just have to worship some idealogy no matter what the context.
@SlayPlenty
@SlayPlenty 3 жыл бұрын
A video on object oriented vs process oriented programming? A GDC talk by the dude that built Thumper argued for it but I cant get my head around the differences. I don't think I've ever built anything purely on functions without classes or objects
@ThinkForward0
@ThinkForward0 4 жыл бұрын
hey Jason , you are my fav unity youtubers , you have great tutorials for beginners like me , i have a question am making a ball stack game where you break the stack and your score increases , when i used singleton , i was able to retain the score after the next level is loaded , but am facing a problem if i lose , it will keep the same stack , while my level spawner spawn new ones which causes Bugs , how can i limit it to the score and not to the stack
@IAmSlex
@IAmSlex 2 жыл бұрын
Need a re-recording of this one day. The audio triggers my condition and I can't seem to make it sit right no matter what I do 😭
@macleodgordon
@macleodgordon 4 жыл бұрын
All I wanted to know was how to use Singletons to create a class which contains global variables. I thought this would address this. Alas, this was such a confusing tutorial and introduced so much stuff which has nothing to do specifically with singletons. While I have enjoyed and learned a lot from your tutorials Jason, this one floors me! :(
@DynaZor
@DynaZor 5 жыл бұрын
What should be the way to hold a small database of items (just example) that would be accessible to all scripts at runtime? I've been using Singletons and had the exact problems you pointed out. I feel like you already have a video answering this but I don't know what to look for... Thank you for all these video!
@DynaZor
@DynaZor 5 жыл бұрын
Thank You!!!
@toxicitzi357
@toxicitzi357 4 жыл бұрын
I have just one little question about the LazyInstantiatedSingleton and AutoCleanupSingleton. Would polimorphism work well with the lines of code that repeat there letter for letter? by which I mean the static _instance and Instance, because the only difference I see in the 2 classes is in AutoCleanupSingleton which has an Awake function. I'm guessing the reason not to use it would be to maintain single responsibility, or, keep the behaviors separated so that if we deleted either, the other one works all the same, so we don't have extra dependencies. I'm asking, because I believe that's an example of duplicate code, just wondering if I'm wrong there.
@dvdmrn
@dvdmrn 2 жыл бұрын
If singletons are not great for global state management, what pattern would you recommend?
@Alevry13
@Alevry13 4 жыл бұрын
Interesting video, I recently started getting into Unity and your channel very helpful for me, thanks! But I got a question, I created abstract class similar to AutoCleanupSingleton with virtual awake with cleanup and I still can add something to Awake if I need to. Is there still any downsides of using it compared to establishing singleton separately in each class i want to use as singleton?
@leozhang1340
@leozhang1340 4 жыл бұрын
what's the VS code extension you used that gives the function arguments a label next to it? Like for example in the beginning of the video, you have Vector3.Distance(a: t.transform.position, b: point)
@THEspindoctor84
@THEspindoctor84 2 жыл бұрын
I believe he is using the JetBrains editor that is made for unity
@Maximetinu117
@Maximetinu117 5 жыл бұрын
Hello Jason, I have a question for you: Don't you think that, in practice, it's better to hide the instance variable by making it private and only exposing static methods manually, that make use of the private static instance variable? In this way you disallow to performs certains operations over the component, transforms or game object, such as destroying it, moving it, adding new components, etc...
@khalreonw
@khalreonw 5 жыл бұрын
I do this and can confirm it's much cleaner. Also another thing i do is making set operations non static, get operation static. That way if something needs to change a field, it will need to reference singleton instance. Another way i use is to make singleton class delegate it's variable changes via interfaces.
@NiclasGleesborg0
@NiclasGleesborg0 5 жыл бұрын
21:00 Been there.. It's a hard balance :P
@gower1973
@gower1973 5 жыл бұрын
I just finished my first game with Unity, but had to use alot of static variables in order to access them from other scripts, but I realize it promotes them to global scope which goes against the whole point of OO design. How do I access variables and functions sitting on another script on another object because even when you make them public Unity just wont compile the script because it says you cant access them. Could you do a tutorial on it?
@kerolesmonsef4179
@kerolesmonsef4179 5 жыл бұрын
Hi Jason i watched all of your pattern design game videos and i still think that i am not good at OOP do you have any suggestion book , videos , ...... tutorials for game development OOP i wonder how did you know all of that and how and from where ? please answer
@sagiziv927
@sagiziv927 5 жыл бұрын
Great tutorial, love your vids, but I have to disagree about this one. I have tried to use Singletons in my last project, but it caused a lot of issues and the code broke pretty often. Usually I had to play with the execution order to make sure the singleton are created before calling them. Nowadays I am using ScriptableObjects and it is much easier and more reliable and flexible. Also I can usually see the current values in the Inspector, unlike with static list/instance so I can debug it a bit quicker
@MrKyokure
@MrKyokure 5 жыл бұрын
How do you refer your SOs without linking them to singleton class?
@sagiziv927
@sagiziv927 5 жыл бұрын
@@MrKyokure I just add reference to them in the script. If there was a good dependency injection options for unity it could be way simpler. The advantage of ScriptableObject vs MonoBehaviour is that the ScriptableObject is related to the project and no the scene, so it will always exist
@MrKyokure
@MrKyokure 5 жыл бұрын
@@sagiziv927 Hmm okay so you add serialized field to hold the SO in every script that needs it? Isn't that too complicated if lets say we have 20 classes that want the reference. Not to mention if we refactor the SO class name, those references will break and we need to re-reference it all.
@sagiziv927
@sagiziv927 5 жыл бұрын
@@MrKyokure Renaming the class name will not break the connection as long as you also rename the file name. About referencing the SO, after you added the variable to the class, If you select class file (in the project view) there is a default values you can assign. so basically I assign it from the beginning and don't need to butter when attaching the class to an object. And as I said, if there was good dependency injection framework in Unity life would be simpler
@ozanunay9257
@ozanunay9257 5 жыл бұрын
You don't, that is how.
@N0biKn0bi
@N0biKn0bi 4 жыл бұрын
Line 11, never forget.
@chloe_the_cat2831
@chloe_the_cat2831 2 жыл бұрын
I really want to watch this video, but the changing volume makes it hard for me to listen and understand. I don't know, maybe it's easier for native English speakers. 🤔
@morneerasmus1789
@morneerasmus1789 5 жыл бұрын
I've been using Scriptable Objects kind of like a singleton, would you propose using singletons instead of scriptable objects? e.g. I have a playerData SO and other scripts call that SO's functions.
@justinwhite2725
@justinwhite2725 5 жыл бұрын
Isn't using scriptavle objects this way nearly exactly the same? You'd still have to stick it on a static variable and initialize it on game startup.
@morneerasmus1789
@morneerasmus1789 5 жыл бұрын
​@@justinwhite2725 Yeah I think you're spot on with that, I've just not been using the static variable, been referencing the SO directly from all the other scripts. I'll give that a shot thanks!
@khalreonw
@khalreonw 5 жыл бұрын
@@justinwhite2725 No. it's not the same it's better. I mean you should not use Singletons at all! when there is scriptable objects for godsake. They could have non static initializer helpers to set some initial values to it but those also should not be static. That way you have a pretty good non singleton design. EX: instead of having rely on AudioManager.Play("ExSong", volume, pitch, audioSource) you could do AudioScheme usedScheme; usedScheme.Play("Exsong"); and set audioSource, volume, pitch from there. You would make init class to set audioSource value but this way you don't have to have single thing doing every kind of operation. That's just very small example. Think of it on an RPG level. Where you have skills and stuff. Singletons would be nightmare on that part. I only have one singleton and that is the game manager. Thats it. Thats all you need. If you are not making a multiplayer game.
@justinwhite2725
@justinwhite2725 5 жыл бұрын
@@khalreonw so how dl you reference it if it's not static? Do you link it into all your monobehaviours? That seens fragile.
@khalreonw
@khalreonw 5 жыл бұрын
@@justinwhite2725 Not all of them. just the ones that would need it. It's faster to debug and understand what's being used and what's not being used.
@pimpace
@pimpace 5 жыл бұрын
Sorry, but what is the purpose of this local _audioSource in AudioManager? There is no AudioSource on this object.... GetComponent is irrelevant here. It is not?
@denispahunov6415
@denispahunov6415 5 жыл бұрын
Why make half an hour video when it all could be represented as a couple pages of text with code samples and all that useful stuff?
@frostden
@frostden 4 жыл бұрын
What would you do if your Singleton needed to have Prefab references in it? I have a 'Spawner' script which provides a static 'Instance' reference to itself. It is on a prefab, and contains references to any Prefab I might want to spawn. It's useful, because if any entity wants to spawn a bullet, fireball, character, etc, they can call the Spawner. It means I don't have to add an 'Explosion' prefab to every Projectile, Spell, and Death animation that might need it, for example. The problem is that the Spawner Prefab contains references to every Prefab that I might want to spawn. I did this to avoid using Resources.Load(). I don't want to use Resources.Load() because 1. I don't want to access prefabs by path, and 2. I don't want to to arrange my project by how a prefab is instantiated, instead of what it does/is. It looks like LoadResource() might be unavoidable in this case. On the bright side, I would only be using it to load the Spawner prefab itself...
@YiffyStiffyMeow
@YiffyStiffyMeow Жыл бұрын
I don't understand. How do you have a prefab instantiated to use the Spawner script if the prefab was never loaded with Resources.Load to begin with? Isn't this a necessary first step to utilising a prefab?
@frostden
@frostden Жыл бұрын
@@YiffyStiffyMeow My comment was made three years ago. "Globals/Singletons/Utilities" in unity were a mess then. I was struggling with them at the time, and built all sorts of horrible workarounds. Hopefully they're less of a mess now. I don't know. I don't work with Unity any more.
@YiffyStiffyMeow
@YiffyStiffyMeow Жыл бұрын
@@frostden No worries, I'm still learning so I can't vouch for if they're fixed or not haha
@9paradox
@9paradox 5 жыл бұрын
One more.. Thread safe Singleton, with help of volatile and synchronisation
@lemetamax
@lemetamax 5 жыл бұрын
I didn't actually complete the video cause the audio was driving me crazy...no offence.
@HunTMovie
@HunTMovie 5 жыл бұрын
Sorry for question but... Are You Negan from The Walking Dead?
@gaming4fun419
@gaming4fun419 5 жыл бұрын
I'd love to have a new video on this subject. I simply can't listen to it =( Don't know if it's my ADHD or what it is but I can't focus at all, and this is something I would love to learn more about.
@sinistermephisto65
@sinistermephisto65 5 жыл бұрын
Tupac Approves this video
@arnovveiss
@arnovveiss 5 жыл бұрын
You are using too much noise reduction for your audio.
@_CloudyBunny
@_CloudyBunny 3 жыл бұрын
English is not my first language. Your voice is unclear and loudness is not constant so I really had a hard time understanding your content!
@KhrisKruel
@KhrisKruel 4 жыл бұрын
So this is nice, but this really doesn't explain why use singletons over static, or vice versa
@gazoonman
@gazoonman 4 жыл бұрын
Yes, I thought it too, the thumbnail was supposed to explain it too
@dankodev
@dankodev Жыл бұрын
Singletons are mid and Scriptable Objects are based
@gregridd
@gregridd 3 жыл бұрын
most of my scripts are singletons :S
@eduoki8498
@eduoki8498 4 жыл бұрын
while(true) => LikeButton();
@thygrrr
@thygrrr 3 жыл бұрын
You should retract this video, it's poor software engineering and you don't "new a property".
@yudanaim6849
@yudanaim6849 4 жыл бұрын
Please replace your cranky microphone you heart my ears. Put a poofer and make it a little less hi mid add more bass and cut the hi ends . It happened during manny of your tutorials and when it happened i just stopped and move on. Man do something about it.
@Unity3dCollege
@Unity3dCollege 4 жыл бұрын
It should be much better in the newest videos :)
@sam_cavalheiro
@sam_cavalheiro Жыл бұрын
Please, fix this microphone
@andremilanimartin3338
@andremilanimartin3338 4 жыл бұрын
pls set up yiut mic properly. my ears are hurting
@Unity3dCollege
@Unity3dCollege 4 жыл бұрын
It's better now... totally new setup :) I'll have to re-record some of these soon.
@The28studio
@The28studio 5 жыл бұрын
I never use Singleton , and I had a fight with coworkers abt it.
Object Pooling (in depth) - Game Programming Patterns in Unity & C#
29:56
Truth about Singletons in Unity | New GameDevs WATCH!
16:49
Jason Weimann
Рет қаралды 17 М.
Zombie Boy Saved My Life 💚
00:29
Alan Chikin Chow
Рет қаралды 29 МЛН
小丑把天使丢游泳池里#short #angel #clown
00:15
Super Beauty team
Рет қаралды 48 МЛН
10 Steps I use to Design and Code Big Game Systems in Unity3D
30:14
Jason Weimann
Рет қаралды 70 М.
Unity Beginners - Static Variables vs Non Static Variables
10:40
Adamant Algorithm
Рет қаралды 3,2 М.
Code Smells - 'Switches' in Unity3d / C# Architecture
25:21
Jason Weimann
Рет қаралды 78 М.
Our Thoughts on Using Singletons in Unity
10:35
Infallible Code
Рет қаралды 17 М.
Singletons in Unity (done right)
7:26
Game Dev Beginner
Рет қаралды 12 М.
Why I don't hate Singletons?
11:30
Jason Weimann
Рет қаралды 26 М.
Assembly Definitions in Unity
7:56
Infallible Code
Рет қаралды 76 М.
The Singleton Design Pattern - Part of the Gang of Four
28:41
IAmTimCorey
Рет қаралды 75 М.
The 8 Game Code & Architecture Mistakes We ALL Make - Unity3D
25:45
Jason Weimann
Рет қаралды 122 М.
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 179 М.
😳 Тест на японца проходит ЯПОНЕЦ ЛАЙКА @samuraika_
0:39
Настя, это где?
Рет қаралды 7 МЛН
Опасная Морская Пена! 🤯
0:25
ШОК
Рет қаралды 2,3 МЛН
ЛОГОПЕД (смешное видео, юмор, приколы, поржать, Вайны)
0:59
Натурал Альбертович
Рет қаралды 2,5 МЛН