Unity C# Beginner Tutorial – Creating a Class and Variable (Part 02) HD 2017 [w/ subs]



00:00:00 – austin and welcome back to game
00:00:02 – programming in unity3d and this part
00:00:05 – we're going to be actually writing a
00:00:07 – c-sharp script it's going to be a very
00:00:09 – very basic one will learn a little bit
00:00:11 – of syntax but we're just going to be
00:00:13 – talking about what a namespace is what a
00:00:16 – class is how to define one how to
00:00:18 – inherit from another class then we'll be
00:00:21 – getting into a variable noon we'll be
00:00:23 – getting into methods all simple stuff
00:00:26 – and all key components of programming
00:00:30 – but if you don't know that stuff I want
00:00:32 – to be able to teach it to you want you
00:00:34 – to know it after this video so let's get
00:00:38 – started when we left off last time we
00:00:41 – had this we didn't do anything we
00:00:44 – created a scripts folder and now I want
00:00:46 – to go and on that scripts folder I want
00:00:48 – to create a c-sharp script we're going
00:00:51 – to working in c-sharp no JavaScript nobu
00:00:54 – script sorry but C sharp is just better
00:00:58 – so we're gonna do that c-sharp script
00:01:01 – we're going to call this demo and over
00:01:04 – here in the inspector now that this is
00:01:06 – selected you can see what this script
00:01:08 – contains but we're going to delete all
00:01:11 – that so i'm going to double-click on
00:01:12 – that actual script it will open it up in
00:01:16 – monodevelop now in this version of unity
00:01:20 – if you have the newer versions you will
00:01:22 – have the new version that's monodevelop
00:01:23 – it's still pretty much sucks but it's
00:01:27 – simple to use it's integrated so use it
00:01:30 – we're going to be deleting all of this
00:01:33 – and typing that up as if it wasn't there
00:01:35 – so the first thing is we're writing a
00:01:38 – script in unity3d now this is live in
00:01:41 – your new 3d so I save this it'll save it
00:01:43 – to that script right and if I attach
00:01:46 – this script to an object as a component
00:01:48 – which we'll get into then this code will
00:01:52 – be attached to that object very
00:01:54 – important very simple and if you've
00:01:56 – heard of object-oriented programming it
00:01:58 – doesn't mean anything more anywhere else
00:02:01 – than it does in unity it is definitely
00:02:02 – object-oriented programming ok so the
00:02:06 – first thing we have to do is we have to
00:02:07 – tell our script our class that we are
00:02:10 – going to be using
00:02:12 – the Unity engine namespace now this
00:02:14 – namespace contains a lot of classes and
00:02:17 – methods and stuff that the unity engine
00:02:20 – provides and we can use that without
00:02:23 – saying that we're using that but we have
00:02:25 – to define the namespace every time we
00:02:27 – use a method from the from the namespace
00:02:30 – and that's not ideal so we'll just tell
00:02:33 – it up front hey we're going to be using
00:02:35 – unity engine and now we don't have to
00:02:39 – define that namespace every time we use
00:02:40 – a method I will explain this a bit more
00:02:43 – when we get into something that actually
00:02:45 – requires that but for now that's the
00:02:47 – only explanation I can give the next
00:02:50 – thing is we're going to be using now
00:02:51 – using is just saying it's using a
00:02:54 – namespace right so whatever you put
00:02:55 – after that will be the namespace you
00:02:57 – views so now we're going to be using
00:03:00 – system dot collections now what system
00:03:04 – but collections gives you access to
00:03:06 – would be arrays hash tables different
00:03:10 – interfaces that we'll be using
00:03:11 – throughout these series now that is set
00:03:14 – up what we have to do now is start a
00:03:15 – class the first thing we have to do is
00:03:18 – determine if the class is going to be a
00:03:20 – public class which means it's accessible
00:03:22 – from other classes right from other
00:03:24 – scripts and we do that with an access
00:03:27 – modifier so this one's gonna be public
00:03:28 – or going to say public now we're going
00:03:30 – to set a class that just says we are
00:03:32 – defining a class here in this case the
00:03:34 – name of the class has to match the name
00:03:36 – of the file which is demo so demo is the
00:03:39 – name of the class now we could just
00:03:41 – block this out and say okay we're done
00:03:43 – with that class the class is it works
00:03:45 – now whenever this class is used from an
00:03:48 – object the stuff inside of it can be
00:03:50 – called we can use the stuff inside this
00:03:52 – class but we have to inherit from mana
00:03:57 – behavior if we want to use a lot of the
00:03:59 – stuff available in the unity engine
00:04:02 – right monitor behavior is a class inside
00:04:05 – of the unity engine name space so the
00:04:09 – way we inherit a class within a class is
00:04:13 – we do a colon and then we say the name
00:04:16 – of that class in this case is mana
00:04:17 – behavior if you get the class right and
00:04:20 – it's built in class then it will use the
00:04:23 – syntax highlighting and light it up blue
00:04:25 – that means you got the class right it
00:04:27 – was one of your own classes it won't
00:04:29 – know that that's a real class so it may
00:04:31 – not be blue right not a big deal but
00:04:34 – what's happening here is monton behavior
00:04:36 – contains a lot of methods a lot of stuff
00:04:40 – that you can use and I have a list over
00:04:41 – here I can show you won't make any sense
00:04:43 – right now but if you want to later on go
00:04:46 – to the documentation and search for mono
00:04:48 – behavior you'll get a list of all the
00:04:50 – functions or methods that are available
00:04:53 – you'll get a list of all this stuff
00:04:55 – that's available and so a lot of stuff
00:04:56 – that you need a lot of stuff you may so
00:04:59 – now the demo class can access the stuff
00:05:01 – with amount of behavior now inheritance
00:05:03 – works like it sounds like it should work
00:05:06 – its inheritance so if demo inherits from
00:05:09 – on our behavior I can make another class
00:05:12 – the inherits from demo and by laws of
00:05:15 – inheritance it will inherit amount of
00:05:18 – behavior right this is how inheritance
00:05:20 – works it cascades down it's very
00:05:23 – important the next thing we're going to
00:05:25 – do is set up a variable now a variables
00:05:27 – like in algebra x equals something right
00:05:30 – so x equals 5 that's a variable now that
00:05:33 – now x holds the value of 5 you can
00:05:37 – modify x and change X but X will always
00:05:41 – have started out equal in five and then
00:05:43 – you modify it right so x holds a value
00:05:45 – of something so at the same thing with
00:05:47 – this but with variables we also have to
00:05:49 – identify whether it's going to be public
00:05:53 – or private now private means it's not
00:05:55 – accessible from outside of its class so
00:05:58 – it'll be accessible within this class
00:05:59 – but outside of it not so much public
00:06:03 – means you can access it from another
00:06:05 – script and it will be available in the
00:06:09 – unity inspector this one will be public
00:06:11 – the type of variable is also important
00:06:15 – this is going to be an integer which is
00:06:17 – a whole number right it can be other
00:06:19 – things but for now we're going to be
00:06:21 – focusing on integer int is how you
00:06:24 – define an integer and next comes the
00:06:27 – name of that variable the name of this
00:06:29 – variable is going to be health so just
00:06:31 – say like player health it's not going to
00:06:34 – do anything but it's definitely going to
00:06:36 – give us something to work with right so
00:06:38 – we have
00:06:39 – public int of health now I said a public
00:06:42 – variable can be available and the unity
00:06:45 – inspector what does that mean so I hit
00:06:47 – ctrl s on this it'll save it we come
00:06:49 – back in here now I have demo here and
00:06:52 – you can see that it's updated over here
00:06:53 – if I create an object now a game object
00:06:56 – in unity will appear in the scene here
00:06:59 – wherever we place it and also in the
00:07:01 – hierarchy and if we click on the object
00:07:05 – will see some information about it I
00:07:07 – want a game object now unity has some
00:07:09 – prefabricated game objects for us to
00:07:12 – work with or prototyping or just getting
00:07:14 – a game done quickly right I want to
00:07:16 – create a cube here's my cube if I double
00:07:19 – click on it and hierarchy it'll take me
00:07:21 – into it just like that you can see over
00:07:25 – here I can see all of the components
00:07:26 – that are attached to this cube
00:07:28 – everything in unity is just an empty
00:07:32 – game object with components attached to
00:07:35 – it so if I deleted all of these
00:07:37 – components you would just see nothing
00:07:40 – but it would still be there they would
00:07:42 – still be in that point of space but it
00:07:44 – wouldn't be anything right it would just
00:07:46 – be a point in space so if I turn off the
00:07:49 – mesh renderer it no longer renders the
00:07:52 – mesh which is the graphic the size you
00:07:55 – saw on that cube and that green is the
00:07:58 – box Collider which is part of the
00:08:00 – physics of unity it's like hitbox you
00:08:03 – think of a hitbox and like an FPS game
00:08:05 – you shoot a bullet and it if it hits the
00:08:08 – the box that surround the player then
00:08:10 – the player gets hit that's what this is
00:08:12 – it detects collisions all around it turn
00:08:16 – that off it goes away welcome ha time
00:08:18 – ago and I could go away all together so
00:08:22 – what we have now is the cube mesh filter
00:08:24 – now that's just if we can change this we
00:08:26 – can change this to us a capsule and if
00:08:28 – we time the mesh renderer back on now
00:08:30 – it's a capsule that's all that is so
00:08:34 – when we have a player so we have a
00:08:36 – player model will use the mesh filter of
00:08:38 – the player model pretty cool not
00:08:40 – transform is a component that is very
00:08:43 – important it contains the position
00:08:45 – rotation and scale information for all
00:08:48 – the objects in unity now this is
00:08:50 – positioned at an odd point
00:08:52 – 0out these positions with XY and z now
00:08:55 – it's at the Z at the zero point of the
00:08:58 – world right here in the middle of the
00:09:00 – grid now the scale is one so it's a
00:09:06 – scale 11 so if I was to make this like a
00:09:08 – tin it would be 10 times the size right
00:09:11 – and it's a scale works rotation works
00:09:13 – the same way with it on the different
00:09:15 – axes x y and z and i can just go here
00:09:19 – and reset this one and that we set it
00:09:21 – back the way it was by default pretty
00:09:24 – cool now we can take this cube and we
00:09:26 – can drop a script on it so we have the
00:09:29 – cube here we can drop a script here we
00:09:31 – can drop a script here well this will do
00:09:32 – is add a component of the demo class so
00:09:36 – drop that they're added the demo script
00:09:38 – to it and we can see the health variable
00:09:40 – here is public it's an integer so it's a
00:09:42 – number starts at zero because it's not
00:09:44 – defined as anything and with it being an
00:09:47 – integer I have this cold rag mouse thing
00:09:50 – that pops up here and I can drag the
00:09:51 – integer set it to a different value I
00:09:54 – could type in two different values of 15
00:09:56 – here's 15 health and then I can take the
00:09:58 – health remove health from it later on
00:10:00 – and when it gets 10 20 he dies we'll do
00:10:03 – that next time that's it for this video
00:10:05 – hope you learned something it was pretty
00:10:08 – quick but I want to get all this out of
00:10:10 – the way so we can get to some fun stuff
00:10:12 – right this stuff is important without
00:10:15 – this stuff you won't be able to do the
00:10:16 – fun stuff


Video Url:
http://youtu.be/rYaVMjK9osQ

Comments

comments