![Game Development with Unity for Web Developers](https://static.frontendmasters.com/assets/courses/2022-05-24-unity/thumb.webp)
Check out a free preview of the full Game Development with Unity for Web Developers course
The "Score Solution" Lesson is part of the full, Game Development with Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:
Nick walks through the solution to the score exercise.
Transcript from the "Score Solution" Lesson
[00:00:00]
>> Hopefully you didn't find that too challenging, it was meant to be pretty easy. Here is the solution, there's just two lines that we needed to add to that, so for score value to be reset to zero, we just need to assign it to be zero. And then to increment the score, we just have to say ScoreValue++, you could also set it to itself +1 but of course ++ is a shorthand common in many languages.
[00:00:33]
So I'm actually going to jump over to unity and write up this script, because it is a script that we're going to need. So if you have not already written this, go to the project Window, right-click, and open the Create menu and choose Create C sharp Script, this will be called Score.
[00:00:59]
And I'll open the script. And we don't need these two namespaces at the top, so I'll get rid of those. And we actually don't need either of the methods that are here, because we're gonna write our own, so I'll write a public void ResetScore method. And actually I needed to create a variable first, so let me go back up to the top here.
[00:01:33]
We need a public integer and that's the ScoreValue property that I mentioned. I'll open curly braces there, we need to get, that is public and then a private set, and the reason this get is public is because the property is public. So, if I were to say, that this were set without the accessor there, that would also be public but I am explicitly saying that is a private set.
[00:02:07]
So now in ResetScore, we want to set that property to be 0, and then we need another method called IncrementScore and there we will say, ScoreValue++. So every time this method is called, score will go up by one, when reset score is called it will get reset to zero.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops