1
00:00:03,650 --> 00:00:10,550
In the last lecture we set up, our scripter will object and we created this string question variable

2
00:00:10,550 --> 00:00:12,470
to hold our question text.

3
00:00:12,470 --> 00:00:19,820
And in the process we gave it a serialized filled attribute which allowed us to change it in the inspector.

4
00:00:19,820 --> 00:00:26,840
But when we come to write our quiz script later on, we're going to need to gain access to this variable

5
00:00:26,840 --> 00:00:29,030
or the data contained within this variable.

6
00:00:29,030 --> 00:00:31,340
So how do we go about doing that?

7
00:00:31,430 --> 00:00:37,190
Well, for this, we use a certain type of method that is commonly called a getter method, because

8
00:00:37,190 --> 00:00:43,370
it allows us to get the data within a private variable and return it to a different script.

9
00:00:43,520 --> 00:00:49,280
So let's go in a bit of a deeper understanding of what a getter method is and why we would use it.

10
00:00:49,580 --> 00:00:57,410
Well, I guess a method will give a script from outside of our class read only access to a private variable.

11
00:00:57,410 --> 00:01:03,170
So for our script to object, we mentioned that we didn't want other classes manipulating the data in

12
00:01:03,170 --> 00:01:08,900
our variable and this will help protect the contents of that private variable on our disk.

13
00:01:09,600 --> 00:01:13,060
So pretty straightforward from a description point of view.

14
00:01:13,080 --> 00:01:15,810
But let's have a look at how we would write it.

15
00:01:16,080 --> 00:01:22,230
So let's create a new method in our script to object, and we're going to call this get question.

16
00:01:22,260 --> 00:01:28,350
However, up until this point in the course, we've only used a very specific type of method where we

17
00:01:28,350 --> 00:01:33,090
would maybe have something like public void get question.

18
00:01:35,360 --> 00:01:42,260
But this doesn't allow us to get the question and return it to another script, because as we can see

19
00:01:42,260 --> 00:01:48,260
from our declaration here, it is public, so it will be visible from other scripts, but it's got a

20
00:01:48,260 --> 00:01:53,360
return type of void and this means it doesn't return anything.

21
00:01:53,690 --> 00:01:58,700
For our question, though, we want it to return a string back to another script.

22
00:01:58,700 --> 00:02:05,630
So what we can do here is instead of having void, we can have a public string get question.

23
00:02:06,320 --> 00:02:10,039
And this will now expect the method to return a string.

24
00:02:10,310 --> 00:02:15,890
As we can see at the moment, we're getting a red squiggly on our method name because it says that not

25
00:02:15,890 --> 00:02:18,710
all code paths return a value.

26
00:02:18,710 --> 00:02:24,980
So unlike a void method where we don't ever return anything, we just perform some logic in here.

27
00:02:25,190 --> 00:02:31,730
In this case, we need to return something back from the method and we can do that using the return

28
00:02:31,730 --> 00:02:32,540
keyword.

29
00:02:32,930 --> 00:02:34,430
And what do we want to return?

30
00:02:34,430 --> 00:02:36,920
What we want to return the question.

31
00:02:37,220 --> 00:02:41,450
So this will now give us that read only access that we're looking for.

32
00:02:41,480 --> 00:02:44,540
We have our private string question variable.

33
00:02:44,720 --> 00:02:50,720
We've given it some amount of write access with our serialized field so we can edit it in the inspector.

34
00:02:50,720 --> 00:02:55,610
But when it comes to other classes accessing this variable, they're going to have to go through this

35
00:02:55,610 --> 00:03:00,590
get question method and this will then return a copy of that question data.

36
00:03:00,980 --> 00:03:05,690
Now hopefully that's all making sense, but to really illustrate the point, what I'm going to do is

37
00:03:05,690 --> 00:03:10,700
I'm going to make a second class in this script and you don't have to follow along with this.

38
00:03:10,700 --> 00:03:14,870
This is purely to show you how this might work later on in the course.

39
00:03:15,500 --> 00:03:23,210
If I make a public class and we'll call this test and we can have multiple classes within the same script,

40
00:03:23,210 --> 00:03:31,190
which can be really handy for testing if I was to get a reference to this question script table object.

41
00:03:32,780 --> 00:03:34,250
And just call it my question.

42
00:03:34,280 --> 00:03:34,820
So.

43
00:03:36,650 --> 00:03:40,340
If I then made a fairly standard generic void method.

44
00:03:40,340 --> 00:03:44,420
So we're just going to call this test as well because it really doesn't matter for the moment.

45
00:03:45,020 --> 00:03:51,950
If I was to access this question, S0 and use the DOT operator to see what variables and methods I have

46
00:03:51,950 --> 00:03:59,690
access to, if I try and type question, I won't have access to my question variable up at the top here.

47
00:04:00,200 --> 00:04:06,470
But you may have noticed that that I do have the get question method and if I cap it off with the parentheses,

48
00:04:06,470 --> 00:04:11,660
everything is fine and we can see if we hover over, it's returning as a string.

49
00:04:11,930 --> 00:04:17,720
So if I also create a variable here of type string called the question text maybe.

50
00:04:19,640 --> 00:04:26,570
I can set this string variable to the result of getting the question out of the script object.

51
00:04:27,020 --> 00:04:28,740
So hopefully that makes a bit of sense.

52
00:04:28,760 --> 00:04:31,040
Don't worry too much about this red squiggly here.

53
00:04:31,040 --> 00:04:34,220
This is just because I've named the method the same as the class.

54
00:04:34,220 --> 00:04:36,620
So if I call this test a this would go away.

55
00:04:37,010 --> 00:04:41,960
But hopefully this gets a method that we've created up here is now making a bit more sense.

56
00:04:42,260 --> 00:04:48,290
We've really just controlled the flow of access to the data within our script for object, which is

57
00:04:48,290 --> 00:04:52,010
really good for protecting it from accidental changes.

58
00:04:52,220 --> 00:04:53,450
So that's all for this one.

59
00:04:53,450 --> 00:04:58,520
Quite a short video, but hopefully this getter method is all making a little bit of sense.

60
00:04:58,520 --> 00:05:04,010
And in the next lecture we're going to finish off our script to object by adding a couple more variables

61
00:05:04,010 --> 00:05:10,700
at the top for storing our answers and also creating some other getter methods to return those answers

62
00:05:10,700 --> 00:05:12,530
to our future scripts.

63
00:05:12,860 --> 00:05:19,340
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

64
00:05:19,340 --> 00:05:20,240
next lecture.

