In the last lecture we set up, our scripter will object and we created this string question variable
to hold our question text.
And in the process we gave it a serialized filled attribute which allowed us to change it in the inspector.
But when we come to write our quiz script later on, we're going to need to gain access to this variable
or the data contained within this variable.
So how do we go about doing that?
Well, for this, we use a certain type of method that is commonly called a getter method, because
it allows us to get the data within a private variable and return it to a different script.
So let's go in a bit of a deeper understanding of what a getter method is and why we would use it.
Well, I guess a method will give a script from outside of our class read only access to a private variable.
So for our script to object, we mentioned that we didn't want other classes manipulating the data in
our variable and this will help protect the contents of that private variable on our disk.
So pretty straightforward from a description point of view.
But let's have a look at how we would write it.
So let's create a new method in our script to object, and we're going to call this get question.
However, up until this point in the course, we've only used a very specific type of method where we
would maybe have something like public void get question.
But this doesn't allow us to get the question and return it to another script, because as we can see
from our declaration here, it is public, so it will be visible from other scripts, but it's got a
return type of void and this means it doesn't return anything.
For our question, though, we want it to return a string back to another script.
So what we can do here is instead of having void, we can have a public string get question.
And this will now expect the method to return a string.
As we can see at the moment, we're getting a red squiggly on our method name because it says that not
all code paths return a value.
So unlike a void method where we don't ever return anything, we just perform some logic in here.
In this case, we need to return something back from the method and we can do that using the return
keyword.
And what do we want to return?
What we want to return the question.
So this will now give us that read only access that we're looking for.
We have our private string question variable.
We've given it some amount of write access with our serialized field so we can edit it in the inspector.
But when it comes to other classes accessing this variable, they're going to have to go through this
get question method and this will then return a copy of that question data.
Now hopefully that's all making sense, but to really illustrate the point, what I'm going to do is
I'm going to make a second class in this script and you don't have to follow along with this.
This is purely to show you how this might work later on in the course.
If I make a public class and we'll call this test and we can have multiple classes within the same script,
which can be really handy for testing if I was to get a reference to this question script table object.
And just call it my question.
So.
If I then made a fairly standard generic void method.
So we're just going to call this test as well because it really doesn't matter for the moment.
If I was to access this question, S0 and use the DOT operator to see what variables and methods I have
access to, if I try and type question, I won't have access to my question variable up at the top here.
But you may have noticed that that I do have the get question method and if I cap it off with the parentheses,
everything is fine and we can see if we hover over, it's returning as a string.
So if I also create a variable here of type string called the question text maybe.
I can set this string variable to the result of getting the question out of the script object.
So hopefully that makes a bit of sense.
Don't worry too much about this red squiggly here.
This is just because I've named the method the same as the class.
So if I call this test a this would go away.
But hopefully this gets a method that we've created up here is now making a bit more sense.
We've really just controlled the flow of access to the data within our script for object, which is
really good for protecting it from accidental changes.
So that's all for this one.
Quite a short video, but hopefully this getter method is all making a little bit of sense.
And in the next lecture we're going to finish off our script to object by adding a couple more variables
at the top for storing our answers and also creating some other getter methods to return those answers
to our future scripts.
So to sign off, I'm going to get rid of this test class that we don't need, and I'll see you in the
next lecture.