Using Inheritance In Unity [Unity Tutorial]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone make here from com3 interactive welcome back to the channel today we're going to be taking a look at the basics of inheritance now what is inheritance there's actually one of the fundamental tributes of object-oriented programming this isn't native to unity itself this can be used in any C sharp application it's extremely useful for helping you cut down on reused cold now we put it very simply what inheritance allows you to do is it allows you to define a child class that could be reused when you inherit all the classes from it and it allows you to extend or modify certain behaviors of the parent class so we'll just jump straight into it and I'll show you aware that we can use this in our games so let's take a look at what I've got here all I've got two scripts one for the base for a piece of armor and another for the base of a weapon so if we have a look at these in Visual Studio we can see that the scriptable objects they have some quite generic fields a single field that's unique to the weapon object and then a few methods which again are very generic that we want to be able to use on every one of our items we have a look at the armor we have exactly the same generic fields a different unique and then again all the same methods and these are just two classes we could have resources that are going to need every one of these fields we could have consumables which again with me all of these fields so you get the idea this could get very large very quickly and all we're doing is repeating ourselves over and over again another example of why inheritance is actually useful is the fact that say we have more than the to say we are 15 scripts that all had these fields and we wanted to add an extra field in that we can just call this fool and we love the beloved set for whether or not item is a legendary item no we can have legendary armors but now we've got to copy and paste this into every one of our classes and if we miss one we may get some errors things may not work as we expect so fixing this is actually very very simple all we need to do is we'll create a nice t-shirt script and we'll call this item open this up in visual studio and we don't need a statin up there methods now this item class isn't going to be the item itself this is going to be the base class with all the items parameters so very simply what we can do we can open up our weapons script we know that we're going to need all of these so we can pass those in the same goals for a reduce durability method repair the item every items gonna have a use functionality and potentially we're always gonna want a buy and sell method so we can throw these all into our items class one thing we do need to make sure which are covering a little bit more detail in just a second if we take note our weapon and our armor are both descriptive objects so we want to make sure that item inherits from scriptable object rather than mono behavior so we can jump back over into our weapon and now a weapon instead of inheriting from scriptable object will inherit from item and we see we get a few warning messages this is because the base class in our instance item takes priority so all of these errors are saying that we have a duplicate name but we can fix that by simply deleting everything out of this class apart from our unique field items or item fields rather and we can do exactly the same with our armor so we'll inherit from item will remove all the generic fields and we'll also remove all the methods so I hope you can already see how much better scripts are locked in there we have our unique fields involve we could also add any unique methods to weapon or armor and every time we need to add a new field we only need to add it once in our items class so we can see that this is still working if we right-click create asset we can choose our weapon or our armor will choose a weapon and we'll just call this sword for example and we can see that we have our generic fields that coming from our item plots and we also have the damage value down at the bottom ear which is inside of our weapon class and we can do exactly the same for our armor piece helmet and we see everything's exactly the same apart from the other damage resistance rather than a damage value and if we just go ahead and as an example remove Owen you is legendry billion we can see that our sword updates to remove his legendary and also our helmet so we can control all these fields from one area but let's just go back to our script and we can see that we inherit a use method now we can populate this in anywhere that we like but not every item is gonna need the same use case for example using Assad we're gonna swing it if we use a health potion we're gonna drink it and we want to increase our health so how do we get around this while still inheriting this use method this is where we can use an abstract class and an abstract method so inside our item class we can tell the compiler that we want item to be abstract currently though this means nothing we need to were use method as abstract as well and what this is gonna do this is gonna force all of our components our scripts that are inheriting from item to contain their own use method so with an abstract method we can't actually have a body meaning we can get rid of our curly brackets and just cut this off with a semicolon so no if we save this and jump back over to our weapon we can see that we get an error that says we do not implement the inherited abstract member item use so we're gonna need to create a use method for weapon an errata so let's go ahead and do that we'll make this a public and we want to override our abstract use method match the return type which is void and call it exactly the same name which is use oops autocorrect and we can copy this method out into our armor as well so now even though we inherit the use method in our weapon we can hardly do whatever we like so we could add the call to swing weapon and then in our armor we could put the call for example apply the damage resistance now just circling background to what I said before about inheriting from scriptable object inside our item class well c-sharp only allows single inheritance meaning you can only inherit from one base class for each parent class but there is a sort of workaround for this wherein we can create a structural hierarchy for our classes so in this instance item class is our base class which inherits from scriptable object so based on the hierarchy because item inherit some scriptable object and weapon inherits from item weapon effectively inherits from scriptable object as wealth and because behind the scenes scriptable object inherits from mano behavior that means weapon will also have monobehaviour features and functions so if you had arrived classes weren't actually scriptable objects they were just standard model behaviors your item class could just inherit from or she'll just inherit from mom or behavior unless of course you don't actually need mono behavior features in these classes then item doesn't need to inherit from anything really so this is a final example we can actually take this one level further if we were to create a new script and we'll call this helmet and we'll all miss up and visual studio and remove our starting up there and this time instead of inheriting from model behavior our item we can actually inherit from Emma so no any new helmet that we create it will have all of our Emma's fields and methods so a helmet will have damage resistance and it would have a use method and it would also contain all of our item fields and methods and you could do this as many times as you like to make your items a bit more customizable it's not strictly advisable to go too deep with inheritance just because it makes things a little bit difficult to follow when you train a debug cold but on the flip side of that it does actually provide a lot of benefits were in you can create base classes and pick and choose which ones are needed for individual items cutting down on a lot of year over used cold making for a much easier to follow up project so I hope you can see how valuable inheritances this is just a very basic overview but I think I've covered some of the key aspects and I hope to see you using this in your games very soon if you've learned something today then drop a like on the video and subscribe to the channel you can also find us over on social media for more bite-size unity and c-sharp tips I've been Mike for com3 Interactive and I'll see you again soon
Info
Channel: Comp-3 Interactive
Views: 2,146
Rating: undefined out of 5
Keywords: comp-3 interactive, comp3 interactive, comp-3interactive, comp3interactive, comp, comp3, comp-3, interactive, unity, unity3d, unity2d, 3d, 2d, materials, easy, how, to, howto, learn, course, series, tutorial, tutorials, game, development, develop, developer, android, mobile, games, programming, programmer, tip, tips, indie, coding, basic, basics, C#, video game, first, material, physics, interface, class, navigating, noob, beginner, intermediate, advanced, professional, guide, introduction, inheritance, inherit
Id: iw1JWGv1gLY
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Fri Apr 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.