To create a score, all we're going to do is go over to the assets go over to scripts, right click Create a new script called score. And we can drag this straight on to our player. So now we see the script is attached to the player, we can double click to open, and we can create a new public, int. School. Remember integers are whole numbers. And because our score is going to be in whole numbers, we will just use an integer.
We don't need a star. And we can go instead of update and just write debug dot log. The score is space, make sure it's not speech quotes plus, and they're just passed in our school. Happy Days. So all we need to do now is called the on trigger. To method which is an inbuilt unity method that just calls when we when, when we run into a Trigger Zone.
So we can write void on trigger, enter, possibly a collider and then close out the colada, ABA and an open curly brackets close curly brackets, press space. And then we've got a colada working so we can actually just run a debug dot log here. colada is working. And let's just see how that works. Let's go over to unity, and let's press play. Awesome.
So if we go over to our console, we see the score is zero, which got called 270 times because we have the update method calling Every single frame, and our score was at zero and Kolata is working. So what happened there is when we went through the gap of this first object here, it triggered the on collision, enter. So we entered the collider of a gap. And because it's trigger, so it's okay ontriggerenter we entered the clutter. Okay, let's call, let's call this function, but we want to actually know if it's the correct clot, because we just don't want it to be calling all the time. So we can actually check for tags.
So we can say, if have a thought game of objects, dot tag, equals, so when you put two equal signs, you're asking the question, does equal one equal sign you're assigning something it equals this for example. So we're saying if other gamer tag equals score, huh Then we want to do something. So if you remember we have in our triangle, our gaps are tagged with the tag score up. Make sure you spell it exactly the same with capitals exactly the same like uppercase lowercase. That's why I said to make it lowercase, and then we can say something as simple as score, plus plus, which means the score will go up. Awesome.
So let's check this out. Let's press play. Let's look at the console. Score zero. Score is one scores two scores three. Awesome.
So now we have the logic behind our score system. Whenever our player bumps into a gap, it will add one to the score and our score just keeps adding up. Fantastic.