Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In this video we're going to take a look at variables throughout this course we'll be covering the complete
fundamentals of C sharp programming.
I'll be working the concepts in as we go.
But if you'd like to master the concepts beforehand or get more experience with C sharp in unity I would
highly encourage you to grab a copy of the unity C sharp Survival Guide.
It's another course created in partnership with Unity Technologies and it's designed to prepare you
for a career with C sharp in unity.
It's a great compliment to this course and any other course you're taking on udemy so let's take a look
at variables.
Variables can be thought of as a box that has information in it and that box of information can change.
It's called a very which means the data can vary it can change the variable.
So an example here is that we could have a speed variable and the reason that we'd want a speed variable
is does it make sense to change this hardcoded value every time.
If I suddenly want to change it so that my player moves 20 meters per second and then later I decide
that I want to go to two meters per second.
Does it make sense to always come back here and update this hardcoded value.
Absolutely not.
So what we can do is we can introduce the concept of variables which will allow us to adjust this value
based on whatever the variable is at the current time.
So to create a variable there are three required components with an optional fourth.
The first component here is a public or private reference.
This variable can either be public or private.
If it's public it means that the outside world can communicate with that variable.
Other scripts and other game objects can know that variable exists.
If it's private only the player knows that variable exists.
The next part is the data type.
Every variable has a data type.
There are four common data types in C sharp.
They're ints which are whole numbers floats which are decimal values billions B O L which stands for
bool.
These are true or false values and then we have string in strings or characters of text.
This is basically saying what type of variable you're creating.
So for a speed variable we could use an integer value or a fluke value.
If I want to be able to go 5.5 meters per second I have to use a float.
If I want to go five meters per second I'll use it for my name.
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
not it's a true or false question.
I'm going to use a ball.
The next thing here.
That's the third requirements and final requirement is every variable has a name without a name.
How do I know what I'm talking about.
Every variable has a name it could be whatever you want but it has a name the optional fourth is a value
assigned optional value assigned so let's take a look here at declaring a variable for a speed and let's
say here that I wanted to support float values I could create a public float for my data type and then
a variable name.
Public floats.
Let's go ahead and say speed and I could leave it like this with a semicolon and it has a default value
of 0 or optionally I could assign it to let's say three point five f the F suffix stands for float.
If you forget the F unity won't compile and it will actually tell you that you have an error you'll
see here that you have some red squiggles resolve it with the suffix.
If I was doing an integer value I could simply just type three and you'll see here it's three point
five giving me red squiggles because it's a float value not an integer value.
So right now I'm setting it to three point five.
F I change the int to a float type and then to actually make use of this variable I just replace the
two with my speed.
So I'm multiplying it by speed.
So speed currently is three point five.
So when we run this line of code it's going to take the value of three point five.
If I save this and hop back into unity you're going to see something pretty cool in unity when you're
working with public variables.
You can see them as well as adjust them in the inspector and what's important to note here is that the
Inspector will override what's in the script in the script this has three point eight or three point
five.
But in my speed in the inspector here it says 10.
So what's going to happen here is we're now going to move 10 meters per second to the left.
And the reason why we're moving left is because I have vector 3 not left.
If I change this to vector three right and save this you'll notice here it still says three point five
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
10 meters to the right.
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
greater is that you can manipulate this value in real time.
Right now I'm moving one point five.
I can speed it up and if I go reverse a negative value we're now going to the left.
So what we're going to do now is map this to user input in the coming videos
if I wanted to have these script value overwrite what's in the inspector here.
I can do that by resetting the script.
There's a gear icon and I choose reset and it will now set it back to three point five.
But as soon as I change it in the inspector the inspector value is going to override what's in the script.
Now that's a public variable.
What happens if I make this private I'm going to save this.
Hop back into unity.
Unity is going to compile and you'll see here that our variable is gone.
If I run the application we still move three point five meters per second to the right but I can't adjust
the value anymore.
And that's because it's a private variable.
Only the player can access it and change the value if it's public.
It means other game objects can potentially manipulate it.
For example in Mario Kart when you collect a power up in it adjust your speed it changes the value because
it's a public variable something to note here that's kind of a best practice to do is to determine early
on whether or not these should be public or private variables.
If it's a private variable you'll typically see an underscore which is a dot net standard for the variable.
The reason we use an underscoring the name here for private variables is so that when you're let's say
hundreds or thousands of lines deep inside of your code you can look at a variable and know that it's
a private variable based on the underscore going forward.
Most of our variables are going to be private unless we see a need for it to be public.
If we need another script to directly modify this value we'll keep it public.
But if we don't see a need for the most part best practices everything should be private.
Last but not least let's say here that I want to keep it private but I want my designers to play around
with the speed and not have to open up the script and change this value every time.
Well we have a solution and that solution is the concept of an attribute.
We can add an attribute above a variable of type serialized field and what that's going to do is serialize
the data so that we can read it in the inspector and not only read it but overwrite it from the inspector.
So you'll see here that our speed variable is back even though it's private.
And if I run the application I can still control the speed values.
So my designers can come into the game and modify the values.
However other game objects and scripts can't touch the value as intended.
So that wraps up some variables for some practice.
Check out the unity see shops survival guide which is filled with tons of challenges on making you a
master at all things C sharp.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.