Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:06,290 --> 00:00:11,720
In this video we're going to take a look at variables throughout this course we'll be covering the complete
2
00:00:11,720 --> 00:00:13,880
fundamentals of C sharp programming.
3
00:00:13,880 --> 00:00:16,820
I'll be working the concepts in as we go.
4
00:00:16,820 --> 00:00:22,220
But if you'd like to master the concepts beforehand or get more experience with C sharp in unity I would
5
00:00:22,220 --> 00:00:26,640
highly encourage you to grab a copy of the unity C sharp Survival Guide.
6
00:00:26,660 --> 00:00:31,550
It's another course created in partnership with Unity Technologies and it's designed to prepare you
7
00:00:31,550 --> 00:00:34,400
for a career with C sharp in unity.
8
00:00:34,400 --> 00:00:39,920
It's a great compliment to this course and any other course you're taking on udemy so let's take a look
9
00:00:39,920 --> 00:00:41,590
at variables.
10
00:00:41,810 --> 00:00:50,110
Variables can be thought of as a box that has information in it and that box of information can change.
11
00:00:50,120 --> 00:00:55,140
It's called a very which means the data can vary it can change the variable.
12
00:00:55,190 --> 00:01:01,280
So an example here is that we could have a speed variable and the reason that we'd want a speed variable
13
00:01:01,460 --> 00:01:05,700
is does it make sense to change this hardcoded value every time.
14
00:01:05,870 --> 00:01:11,450
If I suddenly want to change it so that my player moves 20 meters per second and then later I decide
15
00:01:11,450 --> 00:01:13,910
that I want to go to two meters per second.
16
00:01:13,910 --> 00:01:18,410
Does it make sense to always come back here and update this hardcoded value.
17
00:01:18,410 --> 00:01:19,310
Absolutely not.
18
00:01:19,460 --> 00:01:24,770
So what we can do is we can introduce the concept of variables which will allow us to adjust this value
19
00:01:24,830 --> 00:01:27,960
based on whatever the variable is at the current time.
20
00:01:28,160 --> 00:01:33,270
So to create a variable there are three required components with an optional fourth.
21
00:01:33,650 --> 00:01:38,300
The first component here is a public or private reference.
22
00:01:38,300 --> 00:01:40,590
This variable can either be public or private.
23
00:01:40,670 --> 00:01:44,610
If it's public it means that the outside world can communicate with that variable.
24
00:01:44,690 --> 00:01:48,580
Other scripts and other game objects can know that variable exists.
25
00:01:48,920 --> 00:01:54,100
If it's private only the player knows that variable exists.
26
00:01:54,380 --> 00:01:56,940
The next part is the data type.
27
00:01:57,080 --> 00:01:59,420
Every variable has a data type.
28
00:01:59,420 --> 00:02:02,190
There are four common data types in C sharp.
29
00:02:02,420 --> 00:02:10,220
They're ints which are whole numbers floats which are decimal values billions B O L which stands for
30
00:02:10,220 --> 00:02:10,860
bool.
31
00:02:10,970 --> 00:02:18,130
These are true or false values and then we have string in strings or characters of text.
32
00:02:18,140 --> 00:02:21,660
This is basically saying what type of variable you're creating.
33
00:02:21,680 --> 00:02:25,880
So for a speed variable we could use an integer value or a fluke value.
34
00:02:25,910 --> 00:02:29,900
If I want to be able to go 5.5 meters per second I have to use a float.
35
00:02:29,900 --> 00:02:34,510
If I want to go five meters per second I'll use it for my name.
36
00:02:34,760 --> 00:02:39,800
It's going to be a string of text so I'm going to use a string for a ball that says if I'm awesome or
37
00:02:39,800 --> 00:02:41,690
not it's a true or false question.
38
00:02:41,690 --> 00:02:43,260
I'm going to use a ball.
39
00:02:43,730 --> 00:02:44,570
The next thing here.
40
00:02:44,600 --> 00:02:52,640
That's the third requirements and final requirement is every variable has a name without a name.
41
00:02:52,640 --> 00:02:54,260
How do I know what I'm talking about.
42
00:02:54,290 --> 00:03:00,170
Every variable has a name it could be whatever you want but it has a name the optional fourth is a value
43
00:03:00,230 --> 00:03:08,680
assigned optional value assigned so let's take a look here at declaring a variable for a speed and let's
44
00:03:08,680 --> 00:03:15,640
say here that I wanted to support float values I could create a public float for my data type and then
45
00:03:15,650 --> 00:03:16,630
a variable name.
46
00:03:16,630 --> 00:03:17,710
Public floats.
47
00:03:17,710 --> 00:03:23,890
Let's go ahead and say speed and I could leave it like this with a semicolon and it has a default value
48
00:03:23,890 --> 00:03:35,500
of 0 or optionally I could assign it to let's say three point five f the F suffix stands for float.
49
00:03:35,500 --> 00:03:40,270
If you forget the F unity won't compile and it will actually tell you that you have an error you'll
50
00:03:40,270 --> 00:03:44,620
see here that you have some red squiggles resolve it with the suffix.
51
00:03:44,620 --> 00:03:50,920
If I was doing an integer value I could simply just type three and you'll see here it's three point
52
00:03:50,920 --> 00:03:55,150
five giving me red squiggles because it's a float value not an integer value.
53
00:03:55,150 --> 00:03:57,410
So right now I'm setting it to three point five.
54
00:03:57,420 --> 00:04:05,380
F I change the int to a float type and then to actually make use of this variable I just replace the
55
00:04:05,380 --> 00:04:07,640
two with my speed.
56
00:04:07,870 --> 00:04:09,670
So I'm multiplying it by speed.
57
00:04:09,730 --> 00:04:12,010
So speed currently is three point five.
58
00:04:12,250 --> 00:04:17,050
So when we run this line of code it's going to take the value of three point five.
59
00:04:17,050 --> 00:04:23,560
If I save this and hop back into unity you're going to see something pretty cool in unity when you're
60
00:04:23,560 --> 00:04:25,020
working with public variables.
61
00:04:25,060 --> 00:04:31,540
You can see them as well as adjust them in the inspector and what's important to note here is that the
62
00:04:31,540 --> 00:04:36,880
Inspector will override what's in the script in the script this has three point eight or three point
63
00:04:36,880 --> 00:04:37,510
five.
64
00:04:37,510 --> 00:04:41,410
But in my speed in the inspector here it says 10.
65
00:04:41,590 --> 00:04:47,040
So what's going to happen here is we're now going to move 10 meters per second to the left.
66
00:04:47,110 --> 00:04:50,460
And the reason why we're moving left is because I have vector 3 not left.
67
00:04:50,530 --> 00:04:56,110
If I change this to vector three right and save this you'll notice here it still says three point five
68
00:04:56,120 --> 00:05:07,210
f we're going to move 10 meters to the right so if I play it again you'll see here that we're moving
69
00:05:07,240 --> 00:05:09,290
10 meters to the right.
70
00:05:09,370 --> 00:05:14,660
And if I wanted to I could even slow it down a bit more let's say here one point five and what's even
71
00:05:14,660 --> 00:05:17,720
greater is that you can manipulate this value in real time.
72
00:05:17,720 --> 00:05:19,100
Right now I'm moving one point five.
73
00:05:19,100 --> 00:05:24,730
I can speed it up and if I go reverse a negative value we're now going to the left.
74
00:05:24,770 --> 00:05:29,090
So what we're going to do now is map this to user input in the coming videos
75
00:05:31,960 --> 00:05:37,710
if I wanted to have these script value overwrite what's in the inspector here.
76
00:05:37,870 --> 00:05:40,180
I can do that by resetting the script.
77
00:05:40,180 --> 00:05:45,160
There's a gear icon and I choose reset and it will now set it back to three point five.
78
00:05:45,160 --> 00:05:50,590
But as soon as I change it in the inspector the inspector value is going to override what's in the script.
79
00:05:50,590 --> 00:05:52,040
Now that's a public variable.
80
00:05:52,060 --> 00:05:57,170
What happens if I make this private I'm going to save this.
81
00:05:57,170 --> 00:05:58,960
Hop back into unity.
82
00:05:58,960 --> 00:06:04,290
Unity is going to compile and you'll see here that our variable is gone.
83
00:06:04,400 --> 00:06:10,760
If I run the application we still move three point five meters per second to the right but I can't adjust
84
00:06:10,760 --> 00:06:11,820
the value anymore.
85
00:06:12,020 --> 00:06:14,240
And that's because it's a private variable.
86
00:06:14,240 --> 00:06:18,320
Only the player can access it and change the value if it's public.
87
00:06:18,320 --> 00:06:21,530
It means other game objects can potentially manipulate it.
88
00:06:21,620 --> 00:06:28,790
For example in Mario Kart when you collect a power up in it adjust your speed it changes the value because
89
00:06:28,790 --> 00:06:35,710
it's a public variable something to note here that's kind of a best practice to do is to determine early
90
00:06:35,710 --> 00:06:38,740
on whether or not these should be public or private variables.
91
00:06:38,860 --> 00:06:45,150
If it's a private variable you'll typically see an underscore which is a dot net standard for the variable.
92
00:06:45,220 --> 00:06:50,590
The reason we use an underscoring the name here for private variables is so that when you're let's say
93
00:06:50,590 --> 00:06:56,860
hundreds or thousands of lines deep inside of your code you can look at a variable and know that it's
94
00:06:56,860 --> 00:07:01,360
a private variable based on the underscore going forward.
95
00:07:01,420 --> 00:07:06,100
Most of our variables are going to be private unless we see a need for it to be public.
96
00:07:06,220 --> 00:07:11,060
If we need another script to directly modify this value we'll keep it public.
97
00:07:11,080 --> 00:07:16,520
But if we don't see a need for the most part best practices everything should be private.
98
00:07:16,540 --> 00:07:21,970
Last but not least let's say here that I want to keep it private but I want my designers to play around
99
00:07:21,970 --> 00:07:26,410
with the speed and not have to open up the script and change this value every time.
100
00:07:26,410 --> 00:07:31,070
Well we have a solution and that solution is the concept of an attribute.
101
00:07:31,300 --> 00:07:37,810
We can add an attribute above a variable of type serialized field and what that's going to do is serialize
102
00:07:37,810 --> 00:07:44,680
the data so that we can read it in the inspector and not only read it but overwrite it from the inspector.
103
00:07:44,680 --> 00:07:48,450
So you'll see here that our speed variable is back even though it's private.
104
00:07:48,550 --> 00:07:52,600
And if I run the application I can still control the speed values.
105
00:07:52,600 --> 00:07:56,300
So my designers can come into the game and modify the values.
106
00:07:56,320 --> 00:08:01,870
However other game objects and scripts can't touch the value as intended.
107
00:08:01,870 --> 00:08:05,260
So that wraps up some variables for some practice.
108
00:08:05,260 --> 00:08:09,910
Check out the unity see shops survival guide which is filled with tons of challenges on making you a
109
00:08:09,910 --> 00:08:11,830
master at all things C sharp.
11805
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.