 Have you always wanted to have a flashing effect in your Unity game for warnings? Aesthetics and all that cool stuff without needing to do all the animations and all that hassle Well today is a very fortunate day for you because you just came across the right video for you Hello guys, it's Zach at Crypto Grounds here. Welcome back to another Unity tutorial video If you enjoyed this video make sure you smash the like button for YouTube algorithm And if you also learned something new as well If you want to share this video with your friends who also want to learn something new as well Make sure you do that and subscribe my channel if you're new and turn on that bell for future notifications So like I said earlier, we're basically me changing the background color and the text color We're gonna be creating a flashing effect and this can work for anything Buttons anything with an image component that hat that can change color So I'm going to make an empty game object real quick by clicking that plus sign create empty Set the position on zeros click on that add component And I'm gonna create a new script called flashing effect. You call whatever you would like Okay, I have created the script so we can just double-click on this and it shouldn't open up right to our IDE So I'm gonna start with a clean script so we can get rid of the first two lines since we're not gonna be using anything from the system Collections and system collections generic namespaces. That's what these are and instead We're going to be importing the unity engine UI namespace If you don't know what a namespace is it's basically a package that comes with a bunch of methods and classes and stuff So when we are accessing the unity engine dot UI namespace, we have access to all the Unities related UI stuff such as images Text all that good stuff all that will be inside that namespace back to our scripts here We are inside public class flashing effect and this matches our script name here. So we're gonna be working in here So let's implement our UI first So we're gonna have an image and a text and that's what I just added into the project What we can type here is public image background So this is our background image and we want to make this public because we want to be able to access this Inside the unity editor. Now next we need to add our hello world text in here So what we can do is the same thing make it public Type text and we're gonna name it hello world text. So inside our class. We have an image and a text object Now what I'm also going to do is that since we need to change the color I'm going to introduce two color variables two colors that we want to switch between So our first one is also gonna be public We're gonna make this just red and we're gonna make this a property by doing equal sign and This greater than sign and combine it together and for me it shows up as an arrow It does as a plug-in, but you would have this arrow here and that would make it a property a property in our Situation with the arrow is where we create a variable with read-only access a getter or get That's the key word is where we can return the properties value. That's what we set it to That's where we have the arrow and then whatever we set it to So we're gonna set this color red to Color dot red. So now whenever we access red, it's gonna tell me. Oh, this is equal to color dot red Next I want to add another color. I want to keep it what we have right now, which is white So we're going to set color white Equal to color dot white back to what I was saying about the namespaces earlier I'd like to mention is that text is a class inside the unity engine dot UI namespace See as I'm hovering over text right now for my ID it shows that this is a class, which is kind of what this is, right? It's a public class flashing effect. It's the same thing. We are trying to access the classes inside This certain namespace that unity has created for us So we have all of our variables and properties that we have now We need to actually do stuff with them in order to do that. We need to create an update method To do that we want to do public void If you don't know what the update is then basically it just runs every frame Whatever is inside here. So now we have this update method. We need to add one more thing We're going to make a color method. So this is a void method Void is where we don't return anything. Now if we change this to int update, obviously that'd be a little weird That's where we have to return an integer Now I'm going to show you an example except with color. So instead we're going to making a color Method called public color and we're just going to call this lerp red for example In here, we are going to be basically Determining what color the background and the hello world text is going to be So this is where the flashy effect magic occurs is in here So in this lerp red method, we're going to be seeing this red underline under this curly brace right here That's because the return statement is missing. We're not returning anything which we are required to return a color So we need to return a color and that's going to be we're going to be doing this by switching the color around And how we do that is a method in color class called color dot lerp color dot lerp has three arguments Color a color b. So for the third one, which is t and that is represented for time That is how often we're going to change this now It's a bit more complex than that if we put a constant number such as 0.1 It's not going to repeat. It's going to switch Wants and basically that just determines how fast it's going to go Now how we're going to do this is by plugging an equation and I will get to that later So let's start with this method here. So we want to transition from white to red So for our first color, we're going to put white and for our second one, we're going to put red So now for the time now, this is where the math equation comes into place So in here, since this is a float, we're going to be using unity's math f class Since we need to return a float and math f returns floats And in here, we're going to be using something called sign A sign is basically a wave that goes from negative one to one up and down Of course, it's more complex than that But if you want to learn what the sine wave looks like, then I would highly suggest you to google it since this is not a math lecture Basically, all we need to know is that it acts as a wave So we know that we want to switch between white and red back and forth based on a variable we put in Now this variable is going to be time dot time. So you asked me Why do we want to do time dot time? Why don't you just put one? Now if we do sine of one, we're obviously going to get some funky number if you put it in your calculator The thing is that it's still a constant number So it's only going to be going one direction and then it's going to be done It's going to transfer to the next color and it's going to stop When we use time dot time, we're basically plugging it into that sine wave And it goes up and down from negative one to one. I'll show you guys a graph of this what it looks like So let's say sine of x x is represented for time dot time And this is what it would look like. So as x goes higher as time dot time goes higher It's going to keep fluctuating between negative one and one like that So now what negative one does is that it makes the color go backwards. That's what we want Now if we have it at one, it'll go forward So it's going to alternate between going back and going back to one It's going to keep switching between one or the other So back to our script here. We basically want to Continuously be able to Change the color over time That's how this works. We have returned a color here. Now, what do we do with this? What we can do in our update method is grab our background here And we're going to set the color Equal to lerp red because lerp red is a color We are returning a color. So we're setting this method as a color like this, which is the nice thing It's very easy It's portable Also, you can make this into An expression body, which is the same thing with this arrow right here So now this is an expression body. This only works with one line one line of code, right? So we're only doing one thing here. So it makes things look really clean Now, what do we want for our hello world text? Well, we can do the same thing hello world text dot color Color is a variable in the text class So we wanted to set that to lerp red as well So the issue we're going to experience here is that both the background and the text is red So let's add another color. Let's add public color Purple let's grab this purple from the background here What we can do here is select this background here go to the color in the image component here Click on it And you see these three numbers right here for r g and b We want to copy all these numbers over. So i'm going to do that real quick Okay, so you they don't need to be exactly correct You can round them up to the hundredth if you want or the thousands Decimal But here we have zero point five zero four eight zero eight four. So this is r g and b Now in order to make our own color all we got to do is type the keyword new color And we put in our three values here Zero point five zero point four eight and zero point eight four Hold on hold on. You are not done yet. You're not done yet If you hover over this zero point five here Argument type double is not assignable to parameter type Float So what what does this mean? So if we hover over this color right here, which is a constructor So basically a constructor is a very unique method a constructor is what defines that class Given a certain variables as you can see it's basically constructing what the class is Hence why it's called constructor constructor for color here Says that we need three float variables r g and b. So zero point five However, is not a float. It's a double. However, how do we make it a float? Well, we can do two things we can either cast as a float Where you type in float and wrap it around parentheses like this Or you can type you can put in the prefix f which converts it to a float as well I think this is much easier So make sure you put f in front of all of these numbers here to convert them to floats If you'd like to learn more about what floats and doubles are I'd highly recommend you to check out my c-sharp crash course episode four for variables And you'll learn a lot there as well So now we have our purple color and the neat thing about my ide is that it underlines it with the color that we have here Visual studio if you're using that might do that as well. I honestly don't remember, but we have red purple and white so What we can do here is copy and paste this method and rename this to lurp purple However, that's kind of redundant. We're doing the same thing over and over again And we want to do less work in the long in the long term So what we can do Is get rid of lurp red. We're just going to rename this to lurp And we're going to take in two parameters and that is our before and after color So what we can do here is type in color And this will be our first color. Okay, so right now it's only taking in one variable So remember as I was saying earlier how we right here, these are arguments and these are arguments here as well These are parameters. This is what we take in and what we use inside our method So we want to add a second one. So you put comma color second color Okay, and any method can have any amount of parameters as you need You can even implement. So now we can replace this white and red with our two parameters here Which is first color, which will replace with white and second color will place with red Cool. So now we can start to change things around here So obviously we're going to have to rename lurp red with lurp So now in our lurp method, we need to take in two parameters each or two arguments each So remember our background is going to go from purple to white and back So our first color is going to be purple. So we have our variable here Lurp our first color is purple And our second color is going to be white. So we could just put white here For our second one It's going to change from white to red. So we want our first color to be white And our second color to be red Just like that There you go. We successfully have created a flashing effect in unity. Let's go test it out before we pursue We need to head to our game object here, which I forgot to rename to flashing effect We need to assign our background image and our hello world text Simply just drag it like this and this money per second could just be called hello world text So now when we run start we should see some flashing going on Cool Now it switches between purple and white Satisfying This is going slow. So let's say we want to speed this up. Let's go back to our lurp here And we have timed out time So we can add in a third argument if we want to Our third parameter I'm going to do that and this is going to be a float and this is going to be called speed So what this speed will do is that it's going to multiply time dot time So now the color will change. However often our speed is however more often our speed is So now our lurps are red right here We want to add in a number here. So let's say I want the background to update Uh two times faster and I want the hello world to Flash four times faster So what we're going to see here is that hello world is going to change two times faster than the background because four divided by two is two obviously So we're going to save this go back to our game scene right here and click play. So now you can see It's more often So you can do a lot of stuff here as well Cool If you enjoy this video and if you learned something new make sure you smash the like button for the youtube algorithm Share this video with your friends if you want them to learn something new as well Subscribe to my channel if you enjoyed my content and turn on the bell for future notifications If you want to support my channel, all you got to do is click that join button below Here are the instructions All you got to do is scroll down below the video click that join button and select any of the tiers and join And you will be supporting my channel and if you don't want to do that Then you can check out my patreon at the links in the description below patreon.com slash crypto grounds I have quite a bit of stuff there Including backgrounds merch Discord rolls all that good stuff. Anyways, thank you very much for watching to the very end I hope you guys all have an amazing day and night. Thanks for watching This is zack at crypto grounds and i'll see you guys in the next one. Peace