 Not so fast, helmet. Meanwhile... Hi everyone, it is CryptoGroundz. Welcome back to another video. It has been a very long time since I posted a video on this channel, but I really needed to make a video on this topic because some people were getting confused on some topics regarding single tins. That's these guys, classes, these guys, and mono behaviors. So this video is going to be taking place on a whiteboard. I kind of drew everything out, explaining everything, and I will be talking through all of it. So I recorded this kind of last minute on my phone, so I'm really sorry if my quality isn't the best, but here it is. Okay, so I'm currently in a study room, and I have written everything up on this whiteboard. So right here is our class structure. So for now, let's just focus on this special controller class and this data class. So our controller looking at this is like the center of everything, right? You have settings, upgrade handler, achievements, you a handler, you name it, literally anything else will most likely want to refer to this control class. And I'm going to show an example later on. But for now, just to simplify things, you can see that this controller class has a data object. So it has an object, just an object in here, and I'm sure there's more going on, but just know that for now we need to only focus on this one thing, and it's just this object. This object is this right here. We create this data object, and this data right here includes any properties, fields, methods, and so on that is included in this data class. So in perspective, this is basically a blueprint for data, and right here we are creating an object for data. So now what we can do is that we can change anything that's public or that's legal, such as cars. So in this controller class, we can change, we can get, so get or set cars. You have other classes that are probably want to be accessing this data as well. So instead of just having a data object for every single class and somehow merge them all together, pass information around and all that, which would get messy anyways. Why don't we just have a single data object? Well, that's exactly what we do. Anyways, let's move on to this controller class. Let's get a little deeper look into it. So here is our controller class in a little more blown up picture here, but ignore the surroundings and just focus on this dotted box. So there are three things in this controller class. There is a static object called co, and its type is controller, which is literally the same thing as this up here. So you're creating right here, we are creating a static object of this class that it's in. And then in this private void awake method, I spelled that wrong, ignore that. This private void awake method, what we do is we set co equal to this. And awake is a unity special method that comes from mono behavior. Again, I'll get into detail in a second. And this is called before start. Okay, so this is called very early on when you start playing the game. Next, we have our data object, which I explained earlier. And okay, so let's explain what this mono behavior is. So controller derives from mono behavior. So mono behavior is a base class that provides methods such as start, awake, update, and so on. There are many other ones that you can find on the guide. And basically, in order to use the start, awake and update methods, we must implement this mono behavior base class. So without it, these are just normal methods. They won't start or awake, or this won't update every loop unless you somehow connected to another method or a special kind of method that we don't really use public static void main. But then again, that's we're not going to get into detail. But anyways, my point saying that these methods will not work properly. So let's say that we have a script called controller.cs, which includes a single class that implements mono behavior. We are able to add that script to a game object in the Unity scene. So in your hierarchy, however, if we got rid of this, if we were no longer implementing mono behavior, we cannot do that anymore. This class would no longer associate anything related to unity. It is just a plain C sharp class that implements absolutely nothing. And it just has all this. And it does nothing because there's no public static void main method. But we again, don't need to worry about that. So that's why we have this here, because we wanted to do something in Unity, right? We want something to happen on a week. We want to do something on start or update. Let's take a look at this one to the left, which is going to be our UI handler. This isn't doing anything related to UI at the moment. This could literally be any other class. It could be our settings, upgrade handler, achievements, and so on. Note UI handler does implement mono behavior because we want this script to update the UI in Unity. So that's text, images, other game objects, scroll rex, etc. We're not doing that in this example, but this is just to prove a point. So you see in our update method here, we're trying to do two things. We're trying to set a flow X and a flow Y. The first one being we're trying to access this cars variable. Well, we're doing that for both, but we're doing it two different ways. One is not right and the other is. So let's talk about our data class real quick or our data object that we're trying to reference. So that happens in controller. So here again, here's our object public data data. This notes is non static, but just know that what we're trying to do here is flow X equals controller. Okay, so first let's start a controller. So we do the same thing for both. So we are trying to access this controller class statically. Okay, whenever you try to access a class that isn't an object, you must use the class name. If we were to access the class for data, this would be capital D data instead and just ignore all that and we would just do what we want with that class. However, we're not doing that. We're trying to access the class controller and that must be done statically. So we have our controller up here and let's start the first one. So we have dot co. So we are trying to access the static controller object from our controller class. So it's co. Okay. So this is valid because co is a static object. So this is completely legal. And we want to access our non static data object from our controller. This static controller object contains this data. So then we access our cars variable, which is literally just from the data class. Okay. So first point I made this is good to access another object in another class that are both not static. We use singletons, which I will get to in point six. So let's talk about the second one. What's wrong with this? Why can't we just jump straight into accessing data? Well, here's our issue here. One, we start with this controller, right? This is good. However, since we are trying to access this class statically, data must be static. And in this case, it is not static. So this is not legal. If this was public static data, data. So if we had that static term right before data right here, this would be completely fine. Okay. This would be fine. And we would be able to access cars normally. However, since data, our data object in controller class is not static, this is not legal. Okay. I want to clarify that. Number two, this is illegal data and controller must be static in order to do this. So yeah, just as I explained. And I want to note for number three that this applies for literally any other class and this can be done anywhere else. This could be done in your settings class, achievements, et cetera. So for point four, data right here is not static. So we use single tens right here in order to access this non static variable. And we have to do this because we don't have a single 10. We can only access static variables like this. So we create a static object or a single 10 to bypass that. And since data is inside a controller, which is inside of this static object, this is perfectly legal. And I want to note that if this data object was static, we can use method two. We can use this one, right? However, I like to note that one thing I really like seeing is my variables in the inspector. I like to see what the numbers are behind the scenes while playing the game. If this was static, you can no longer view the variables in the unity inspector and that just sucks. So single tens one to one. This is what it is. This is a very, very basic single 10. It gets more complex, but this is just as basic as it can get. And that's honestly what I do for mine. So a few more things I want to point out is that this term right here, this is basically this instance. You're basically saying it as like this instance of controller. So this exact instance, which you assign to a game object in your unity scene. So if you dragged your controller script or added it as a component to a game object in unity, that would be this, right? That component in that game object is literally this, if that makes sense. It's kind of weird saying it, but it literally means that. Okay. Point number two, you can only have one class in your script that derives from mono behavior. You can literally have controller class in one script. You can add another class below it that doesn't derive mono behavior. Heck, you can even put classes inside another class that doesn't derive mono behavior. That is legal and that can be done in one script. However, you cannot have two classes that both implement mono behavior. Unity would get really confused and it just doesn't work that way. It's also the same reason why your script name must be the same name as your class that implements mono behavior. If it was different, Unity would get all confused and it will just break. And I want to quickly point out that I'm not encouraging you to add a bunch of classes within one script. I want you to split it up. For example, I have my controller class. I have upgrade handler. I have UI handler, achievement settings. These are all going to implement mono behavior. Each of these are one, its own class, two, its own script. One thing I've kind of failed on you guys is telling the difference between a class and a script. Because they're very different. I mean, a script contains a class or more. But again, it must follow the rules I explained earlier. I'm going to remind you, because this needs to stick, that each script must have zero or one class that implements mono behavior. Now note, if it has zero classes that implement mono behavior, just like a script called data, this cannot be added to your Unity scene. Or you cannot use Unity special methods such as Awake, Start, and Update as I said earlier. So you can have a script that is just data. This is fine. You can have a script that is just controller. You can also add other classes that don't implement mono behavior. And that will be in one script, and that is good. Just note, if you have two classes that implement mono behavior, things are going to be weird and not work properly. To kind of clear up this data, I probably should have mentioned this earlier, but this is an object. So you have your floats, doubles, right? Those are variables. This is basically a variable for a class. You can have as many of these as you want. You can have data one, data two, you can have different types of data, right? Not saying it's necessary, but just saying that our class is a blueprint and we can create as many of these as we want. This is just one object that is type data. So for how we set up our class, we can get set or read and write this variable cars because it's public. Last point, I'm going to make number six. So right here, this is our single 10. And I want to point out, I've probably already mentioned it, but I'm going to mention it again because it is important. It is static. It allows us to do controller.co. It allows us to access this static controller object and we only use the single 10 outside of this controller class. You don't use it inside yourself. There's no point in doing that. You can literally access data inside this awake method right now and you'll be just fine. However, if you want to access this non static object or any variable or possibly any public method outside of controller, you must use a single 10 just like this. So I am currently editing the video and I just want to jump in and explain why we use single 10s and how they actually work. So if we take a look at where we're at in the video, if we see this public static controller co static object, basically this is being assigned to itself. And this co is an object that is type controller. So this is the controller class and we are making a copy of it called co. Okay. So here's our class called controller. This controller has a non static object called data. And just, just like data includes a float variable called cars, controller has an object called data and it's type data. And so if we step back, controller.co.data. So we are accessing our static object by doing controller.co. And we are able to access or modify any type of variable objects or even call methods that are inside of the controller class. An example. And in our case, it would be the data object. So that's just how we do it. I hope that kind of, if you were still confused at this point, I hope that made a little more sense. Single 10s are strange initially, but once you use them more. And if you want to rewatch this a few times just so it clicks or if you want to do more research, that may help out as well. But they are very powerful and I personally use them all the time. So I know this was a pretty long video and I really hope this clears things up. I know these graphics are a bit confusing, but I wanted to clear this up. And yeah, anyways, thank you for watching. Hope this was really helpful and leave a like if you thought it was helpful. If you have any questions, let me know in the comments below. And I'll see you guys in the next one in the undetermined future. Goodbye.