 Hello guys, CryptoGrants here and welcome back to another video. Today I'm going to be teaching you how to access other scripts without having to initialize them or such as dragging them into your little script boxes here and we'll be doing this by using singletons. All right if you enjoyed this video make sure you smash the like button and subscribe to my channel for more content and turn on the bell for future notifications and videos. Also I just wanted to say sorry for the lack of uploads lately. I've been very busy with school and game development lately so I've been kind of just putting YouTube at a low priority but I'll get back into it. Anyways just hop right into this tutorial. So here we have a canvas with two text objects here and we also have a script empty game object here. Now this is where all of our scripts are going to go okay so we're going to create two new empty game objects okay and we'll just call this script one. Just know that this method can be used for as many scripts as possible so we can literally do this with three scripts if you want okay so now we're going to create a new script called script one and then we'll create a new script in our script two game object called script two. So let's start with script one okay so in here we are in our empty class here and what we're going to do is add our text object in here so we're just going to call this text one for example and we also need to include the using unityengine.ui namespace. This is all we're going to do for now so now when we run this we're going to get a new reference until we drag in this from script one text into our text one object into here. So we're still going to have to do this but I'm going to show you some really cool things too that you can do with this. So let's do the same thing with script two. So we're pretty much just going to copy and paste this just replace the class name with script two which is in script name and we're going to call this text object called text two. We can delete this top lines because we don't really need them. So here's script one and here's script two. Same thing in here we're going to drag our from script two into our text two right here. Okay so now when you run this it should just remain as new text and new text nothing special should happen. So let's say I want to change what text two says in script one or let's just say we have a variable here and we're just going to set that in our start method. So in our public void start method we're going to set number equal to two and in here in our public void updates we're going to display our text two text as as two it's going to display two. We're going to use a thing called string interpolation where we put the dollar sign and then the parentheses and curly braces inside and we could put our variable in here. So we're just going to put something like number colon number. So now this will display something like this number two. So this is what we'll see. So now if we run this we should see our script text two show number two. Perfect. Okay so now let's say we want to change this variable for some reason in a different script. We can't just go to script two dot number okay it doesn't work that way it's because it's not a static class and now the problem with making static classes is that it kind of breaks with our mono behavior here and we cannot access it. We can't drag any of our text and UI stuff in here. So how do we fix that? Single tens. That's the beautiful things about single tens is that we don't have to mess with any of this. So how do we create it? Well first off we need to create instance of its class. So how we do that is that we call public static script two and we're just going to call this instance. So basically we're creating a static instance of this object of its own object. So now if we try to do script two dot instance dot number okay we can set text one we can set the text in script one equal to this number however there's gonna be an issue with this and I will show you once we get there. So let's say we want our script instance number to print here. Okay so let's get rid of this right here this public void start. I'm just gonna comment this out. Let's say we want to assign this number in our script one instead using the start method. So I'm gonna set script two dot instance dot number and this is to three this time. Okay so now both texts should say three however it's not going to succeed we're gonna get a no reference error yeah we're gonna get no reference error it's because we have not assigned this instance to its own object yet. So how do we do this? Well we do a pretty cool thing basically we're assigning the instance to its own object because right now it's just saying that it's just an empty script too so what we're gonna do here is we're gonna make a private void awake method so we want this to call before the start method that's what exactly what awake does and we're gonna use make this an expression body using this arrow here and we're just gonna say instance equals this okay so now we have defined what instance is it should work just fine cool so now if we increment this number in here so now if we assign this if we adjust this number in here in the update method dot number plus plus you'll see it change for both of them cool just like that and another thing is that this is a very basic single 10 there's a lot of complex ways to do this like to make sure it doesn't destroy if it's null or just there's a bunch of complex ones out there so if you want a more complex version which is mostly used for like instantiating 3d objects or stuff like that or models or just some complex things like that you will want to check that out on google anyways i hope that you guys enjoyed this video if you learned something new maybe smash that button subscribe to my channel if you're new and turn notifications for future live streams and videos catch you guys in the next one peace