 In this video I'm going to be showing you how to access variables and other scripts in a much easier way by using Singletons. I'll be explaining you what they are specifically and how to actually use them and create them. Hello everyone it is CryptoGround here welcome back to another Unity OuterGames tutorial video this is episode 2.1 and this is part of the 2021 series. Like I said earlier I'm going to be explaining what Singletons exactly are and how to make them and how they can be a huge lifesaver. Anyways if you enjoyed this video if you learned something new make sure you smash like button subscribe to my channel if you're new and turn on the bell for future notifications of videos and live streams anyways just hop right into it. So first of all we're gonna need to open our controller and our upgrades manager so let's do that. I'd like to kind of explain what Singletons are. So Singletons is more of a concept rather than an actual thing that you make like it's not just like labeled Singleton right like you have a class it's called a class and it's not like that okay it's it's a concept really. So basically what it does is that it prevents you from having to create these right here we have to drag your scripts into the manager in here in your controller script like right here we have the upgrades manager and the controller here it'll basically just eliminate that process which will be a huge lifesaver. So the first thing we're gonna do is go to our controller delete this first line right here the upgrades manager we're gonna get rid of that and go to our upgrades manager and we're gonna do the exact same thing get rid of controller. So real quick I'm gonna show you why we want to create a Singleton and exactly what it does and how to make it. So first of all let's just say we want to access data inside our upgrades manager. So in here previously we would have to make a controller object in here that we're gonna have to just assign in Unity. So to prevent that what we could do is just call controller.data. Now the problem here so let's just set this to new data for example the problem here is that data is non static and we're trying to access it statically okay that's what this is doing right here so if we make the static right here static data this will work but the issue here is that so how do we fix that well let's get rid of this here let's get rid of the static here and all we gotta do is create a static instance of our controller script in here our controller class so we just do public static controller instance that's all you do not yet we have to assign this and we're gonna assign this before anything okay before even start is called so you're probably asking well how do we do that well there's a secret method called awake which is called before start so now we're just gonna create a private void awake and in here we're just gonna set instance equals to this which is this class this object here and we can create this into an expression body which is the arrow like that and that is all we need to do so now what we do in instance here is that we can just do controller dot instance dot data and that will work just fine so now if we were to replace our controllers here because we're gonna have some errors here all we need to do here is do controller dot instance dot data cool now another thing we can do is just create temporary variable such as data and we can set it equal to controller dot instance dot data now this will make it so we have to type controller dot instance a million times now we're only doing it once but I'm just gonna keep it like this anyways otherwise we can just do controller dot instance dot data and we can do the rest here and since we're doing controller here quite a few times I'm just gonna create this variable here and just do and just get rid of these controllers here like that so now let's do the same thing for upgrades manager so again we want to create a public static upgrades manager instance and we're going to sign it to this this class right here on a week like that so now when we go back to here instead of doing upgrades manager we could just do upgrades manager dot instance dot start upgrade manager that is it that's literally all that is so anyways if that was easy enough and if you enjoyed this video and if you learned something new make sure you smash like button consider subscribing if you're new and turn on the bell for future notifications of videos and livestreams anyways if you want to support the channel check out the patreon link in the description below and maybe considered becoming a youtube member just click that join button below anyways thank you everyone for watching and thank you guys all for your support thank you to all my patreon members and my youtube members I'll see you guys in the next one have a great day and night peace