One of the most important principles of OOP is single responsibility. Try to make "as many scripts as you can" following the single responsibility rule. Try to make one script take care of one task only. For instance, for my player I have one script to control its particles, another to control sound effects, another to control animations, another to control movement, another to control death, and so on... That way it's easier to: 1 - Find bugs and fix them 2 - Add new things, a.k.a scalability (really important creating games) 3 - Organize things 4 - Being able to take off things without breaking everything 5 - It will be much easier to come back to old scripts that you don't see for ages 6 - People will think "This guy knows his OOP!" Instead of building a Jenga tower, you just lay the piece down the floor side by side. That way there's no fragile tower to fall, and things as just better. (that's an answer to another comment here...)
Exactly, this has been my method. It’s important to keep track of what does what. Don’t underestimate adding comments next to lines of code either just in case you still get lost
I don't understand constructors... As a visual learner of how what code does what in real game, I don't understand why you have three projectiles (there's only one type of projectile in the examplew which is the watermelon) and what the whole point of the script example is, it seems so detached from the example game.
Constructors just let you ablity to set variables values when creating class object. You may use empty constructor and then set variables manually. Three projectiles are just example, it could be projectile speed/damage etc.
It's just code that runs when the thing is created. To create an instance of a prefab, you click-drag it into the scene, or use Instantiate in code. To create an instance of a component, you click-drag it onto a GameObject, or use AddComponent in code. The create an instance of a C# class, you use 'new ClassName()' in code, which also runs the ClassName() function (constructor). Prefabs/components/monobehaviours do not have constructors.
Being that you're trying to teach, you probably shouldn't use variable names like prA prB fu, etc. It's extremely confusing to newcomers. When you teach programming concepts, always use variable names that clearly communicate intent.
Yeah suddenly it s like "oh ok we can do that" But i have a question could you give me some examples of when it is good to use those "inner classes" please ? Because what he was just saying before was to create separated scripts and just after that he create a class inside a class. Why wouldn't we create a script for this class and use a reference to access it ? I understand that maybe in this example he just did it to show us a new thing and that maybe he didn't have to and could have gone with a different script instead. But then, when do we go for an inner class? Is it just an architectural choice ? Is there a use for it ?
Hmm and so far I've been trying to have as many things as possible inside a one single script. The reason being that I thought and perhaps I read somewhere ( I don't remember exactly ) that having less scripts can be beneficial to the performance of the game. Though, the impact may be minuscule... but still till now I've been trying to have as less scripts as possible. But I think that it might not be a good idea if there are more than one people involved in the development unlike my case. But anyways, I think I would try to have more scripts/classes from now on.
Oh don't do that, for your sake! One of the most important principal of OOP is single responsibility. You should do the opposite of what you're doing. Try do make as many script as you can following the single responsibility rule. Try to make one script take care of one task. For instance, for my player I have one script to control it's particles, another to control sound effects, another to control animations, another to control movement, another to control death and so on... That way it's easier to: 1 - Find bugs and fix them 2 - Add new things, a.k.a scalability (really important creating games) 3 - Organize things 4 - Being able to take off things without breaking everything 5 - It will be much easier to come back to old scripts that you don't see for ages 6 - People will think "This guy know his OOP!" Instead of building a jenga tower, you just lay the piece down the over side by side. That way there's no fragile tower to fall, and things as just better.
@@leonardofraga5130 Yeah I was doing it totally the messy way. Glad I came to know about my mistake. Thanks for confirming this as earlier I was still little confused. This is something useful I learnt about a good programming practice :D
Yeah, for performance it;s better to have one big script, but it's not very convenient for organizing your code. This is why new ECS approach will be added in Unity soon. It will help both performance and script composition.
Hey so I might be completely dumb but I have a hard time understanding how useful classes can be...can’t you just do everything you did in a “normal” script? And then just apply that script to every object you want in your scene? (I’m a complete noob btw!)
A class is intendet to be a template, not an actual object. A class is like the mold for your cake. The constructor is a method that enables you to create instances of yor class (like, children). Those instances are called objects. An object is the actual piece of code that you wil be running. The advantage of having a class is that it lets you create as many objects as you like. All of that objects will inherit the properties defined in the class, and later you will be able to change those properties at an object level. Let's say you have a class defined to represent a vehicle, it will have properties as color, brand, weigth, etc. You then create the objects and all of them will have those same properties, but you will be able to change let's say the color for every one of them, while maintaning the other ones untouched.
i was confused as to how he could pipe projectileA into the same class twice. prA, prB, prC in one, and prA and fuel into the other. He is putting 50 into both projectileA slots, so i wondered what would happen if you change one of those 50s? Which one will it do first? BUUTevery time he calls it, hes storing it into a different variable (myStuff/myOtherStuff) So if you change the numbers going into both, it doesnt matter, because youre calling on it with different variables, so theyre actually 2 different things entirely. I guess thats what instance means.. Anyways just thought id share cus that confused me initially.
My issue with making multiple scripts is how they're linked. For example I have a weapon, it's taken into a weapon script. But the weapon script also moves the player forward a little. So I would need to access the movement variable in the movement script as well in order to make the attack move forward
magnusm4 you can access other scripts/classes with getcomponent (this channel has a video about it). Make sure the variables you want to use from other scripts are public variables
This video is really well explained. Thanks a lot! How do I know when I should do sub classes or just create totally new classes in another script for the same funcionality?
To anyone watching this feeling confused/frustrated, just look up Raja Biswas/Charger Games. He is the best at explaining coding in Unity for beginners.
When ever I type this same thing without a mistake like copy paste it's says full of error It made a huge headache I ve typed this in mono develop (Built in unity) Somebody else help PLZ
I'm planing to uses this in my classroom. I hope "Teaching Game Design and Development" and "Create with Code" is not getting removed for a couple of years?
I feel like you should have dedicated this video to classes and used a practical example. instead of jumping into an array of constructors and throwing a bunch of new terms around without actually explaining what a class is and how it's used. You pretty much only explained why it's used.
That's cool and all, but how do I create a *separate or different script of a class* ? It gives me the same issue I had, not wanting to create the same methods all over again, since I'm making a game which it has lots of players. For now I will stick with copying and pasting the same code until I get an answer that it works fine.
Inheritance might be what you are looking for if i understand you correctly. Like this you can build a base class with the basic functions that every script has and then extend on it.
First crwate a public class named "public class stuff" then give the class some attributes like..public string name, public int roll no. Etc. The whole concept of classes is to create custom data types according to your needs. Watch freecodechamp. Org c# tutorial for better clarity.
It's certainly possible. You can find some tutorials out there from other creators on how to do so, like this one: gamedevtraum.com/en/programming/basic-programming/communication-between-scripts-examples-in-unity/
"its recommended to think about how your scripts will be laid out before you go make a big class" lol... I made my game manager class 2k lines whoopsuhdaisy Spaghetti Monsters are people too Aware
i'm completely lost, its so weird. In this context, i realize i don't understand the words "constructor", "argument" and "object". I don't really understand what "new'" does or what it means to create an instance either, even though those are all words i'm very familiar with in other context. In the end, what are we supposed to learn from this ?
this is probably the worst way to teach programming... just droning on while writing lines of code without any visual examples to complement the information or show why the engine demands the use of classes and constructors. It's an extremely lazy way to teach and very confusing for beginners.
One of the most important principles of OOP is single responsibility. Try to make "as many scripts as you can" following the single responsibility rule. Try to make one script take care of one task only. For instance, for my player I have one script to control its particles, another to control sound effects, another to control animations, another to control movement, another to control death, and so on...
That way it's easier to:
1 - Find bugs and fix them
2 - Add new things, a.k.a scalability (really important creating games)
3 - Organize things
4 - Being able to take off things without breaking everything
5 - It will be much easier to come back to old scripts that you don't see for ages
6 - People will think "This guy knows his OOP!"
Instead of building a Jenga tower, you just lay the piece down the floor side by side. That way there's no fragile tower to fall, and things as just better.
(that's an answer to another comment here...)
Well said ... thanks for the advice ♥
wow man. nice
Really good advice!
thanks dad
Exactly, this has been my method. It’s important to keep track of what does what. Don’t underestimate adding comments next to lines of code either just in case you still get lost
I don't understand constructors... As a visual learner of how what code does what in real game, I don't understand why you have three projectiles (there's only one type of projectile in the examplew which is the watermelon) and what the whole point of the script example is, it seems so detached from the example game.
Constructors just let you ablity to set variables values when creating class object. You may use empty constructor and then set variables manually. Three projectiles are just example, it could be projectile speed/damage etc.
It's just code that runs when the thing is created.
To create an instance of a prefab, you click-drag it into the scene, or use Instantiate in code.
To create an instance of a component, you click-drag it onto a GameObject, or use AddComponent in code.
The create an instance of a C# class, you use 'new ClassName()' in code, which also runs the ClassName() function (constructor).
Prefabs/components/monobehaviours do not have constructors.
Being that you're trying to teach, you probably shouldn't use variable names like prA prB fu, etc. It's extremely confusing to newcomers. When you teach programming concepts, always use variable names that clearly communicate intent.
Thats cool but this is a part of a series and he even says its intermediate so a beginner really shouldnt even be here.
@@mrhandexists20 the video title is “Beginner scripting tutorial”.
Uses an inner class; doesn't mention what he's doing or his rationale for doing so.
Yeah suddenly it s like "oh ok we can do that" But i have a question could you give me some examples of when it is good to use those "inner classes" please ? Because what he was just saying before was to create separated scripts and just after that he create a class inside a class. Why wouldn't we create a script for this class and use a reference to access it ?
I understand that maybe in this example he just did it to show us a new thing and that maybe he didn't have to and could have gone with a different script instead. But then, when do we go for an inner class? Is it just an architectural choice ? Is there a use for it ?
So it's a bit like how Instantiate has different parameters based on what variables you give it? That's cool!
Hmm and so far I've been trying to have as many things as possible inside a one single script. The reason being that I thought and perhaps I read somewhere ( I don't remember exactly ) that having less scripts can be beneficial to the performance of the game. Though, the impact may be minuscule... but still till now I've been trying to have as less scripts as possible. But I think that it might not be a good idea if there are more than one people involved in the development unlike my case.
But anyways, I think I would try to have more scripts/classes from now on.
Oh don't do that, for your sake!
One of the most important principal of OOP is single responsibility. You should do the opposite of what you're doing. Try do make as many script as you can following the single responsibility rule. Try to make one script take care of one task. For instance, for my player I have one script to control it's particles, another to control sound effects, another to control animations, another to control movement, another to control death and so on...
That way it's easier to:
1 - Find bugs and fix them
2 - Add new things, a.k.a scalability (really important creating games)
3 - Organize things
4 - Being able to take off things without breaking everything
5 - It will be much easier to come back to old scripts that you don't see for ages
6 - People will think "This guy know his OOP!"
Instead of building a jenga tower, you just lay the piece down the over side by side. That way there's no fragile tower to fall, and things as just better.
@@leonardofraga5130 Yeah I was doing it totally the messy way. Glad I came to know about my mistake. Thanks for confirming this as earlier I was still little confused.
This is something useful I learnt about a good programming practice :D
Yeah, for performance it;s better to have one big script, but it's not very convenient for organizing your code. This is why new ECS approach will be added in Unity soon. It will help both performance and script composition.
@@dtr0q10 So... did they implement it?
Hey so I might be completely dumb but I have a hard time understanding how useful classes can be...can’t you just do everything you did in a “normal” script? And then just apply that script to every object you want in your scene? (I’m a complete noob btw!)
A class is intendet to be a template, not an actual object. A class is like the mold for your cake. The constructor is a method that enables you to create instances of yor class (like, children).
Those instances are called objects. An object is the actual piece of code that you wil be running.
The advantage of having a class is that it lets you create as many objects as you like. All of that objects will inherit the properties defined in the class, and later you will be able to change those properties at an object level. Let's say you have a class defined to represent a vehicle, it will have properties as color, brand, weigth, etc. You then create the objects and all of them will have those same properties, but you will be able to change let's say the color for every one of them, while maintaning the other ones untouched.
i was confused as to how he could pipe projectileA into the same class twice.
prA, prB, prC in one, and
prA and fuel into the other.
He is putting 50 into both projectileA slots, so i wondered what would happen if you change one of those 50s? Which one will it do first?
BUUTevery time he calls it, hes storing it into a different variable (myStuff/myOtherStuff)
So if you change the numbers going into both, it doesnt matter, because youre calling on it with different variables, so theyre actually 2 different things entirely. I guess thats what instance means..
Anyways just thought id share cus that confused me initially.
My issue with making multiple scripts is how they're linked. For example I have a weapon, it's taken into a weapon script. But the weapon script also moves the player forward a little.
So I would need to access the movement variable in the movement script as well in order to make the attack move forward
magnusm4 you can access other scripts/classes with getcomponent (this channel has a video about it). Make sure the variables you want to use from other scripts are public variables
Wow, one of the best lecture
/tutorial I ever watch, some confusing things are solved immediately. Thank you very much.
i cannot blieve that a unity tutorial has ended up making oop make sense for me haha
I have no idea how to start that so im gonna watch more
"Shoot small objects"
*yeets a giant watermelon*
This video is really well explained. Thanks a lot! How do I know when I should do sub classes or just create totally new classes in another script for the same funcionality?
Was hoping to see this explained in the comments as well
If it's so well explained, why are you asking a question?
@@35matinee courtesy
To anyone watching this feeling confused/frustrated, just look up Raja Biswas/Charger Games. He is the best at explaining coding in Unity for beginners.
When ever I type this same thing without a mistake like copy paste it's says full of error
It made a huge headache
I ve typed this in mono develop
(Built in unity)
Somebody else help
PLZ
I'm planing to uses this in my classroom. I hope "Teaching Game Design and Development" and "Create with Code" is not getting removed for a couple of years?
I feel like you should have dedicated this video to classes and used a practical example. instead of jumping into an array of constructors and throwing a bunch of new terms around without actually explaining what a class is and how it's used. You pretty much only explained why it's used.
what should I do if unity don't automatically put public classes in the script for me?
Sure thing, I just put everything in another script...
How do I find ae. the stats attached to a weapon that is a child of my player?
float x = GetComponentInChild();
stats.attack/defense/etc...etc...
That's cool and all, but how do I create a *separate or different script of a class* ? It gives me the same issue I had, not wanting to create the same methods all over again, since I'm making a game which it has lots of players. For now I will stick with copying and pasting the same code until I get an answer that it works fine.
Inheritance might be what you are looking for if i understand you correctly. Like this you can build a base class with the basic functions that every script has and then extend on it.
I see you are fan of "SPACE" and not "TAB" ///(
You know he using a class inside of a class because it will show you how to use a constructor instead of switching views to another IDE window.
I did the public Stuff mystuff = new Stuff(); and my command log said the line is causing a stack overflow. Class instances are confusing to me.
First crwate a public class named "public class stuff" then give the class some attributes like..public string name, public int roll no. Etc. The whole concept of classes is to create custom data types according to your needs. Watch freecodechamp. Org c# tutorial for better clarity.
Do we really need the third constructor ?
The one below the //constructor comment
5:46
I think so, because it gives the variables a default value instead of NULL.
Why not just doing without class? is the same thing
huh?
@@gregoryfenn1462 I don't get the thing to make class in another class
You know he using a class inside of a class because it will show you how to use a constructor instead of switching views to another IDE window.
Very good
any way to get communication across scripts?
It's certainly possible. You can find some tutorials out there from other creators on how to do so, like this one: gamedevtraum.com/en/programming/basic-programming/communication-between-scripts-examples-in-unity/
@@unity thank you so much
@@timothygooding9544 Happy to help!
Why is it light mode!?!?!?!?
"its recommended to think about how your scripts will be laid out before you go make a big class" lol... I made my game manager class 2k lines whoopsuhdaisy Spaghetti Monsters are people too Aware
Not to be nit picky, but those aren’t brackets
i'm completely lost, its so weird.
In this context, i realize i don't understand the words "constructor", "argument" and "object".
I don't really understand what "new'" does or what it means to create an instance either, even though those are all words i'm very familiar with in other context.
In the end, what are we supposed to learn from this ?
Thanks.
I didnt understand, and dont make sense.
01:05 önemli
cofusing
small brain go brrr
english please
this is probably the worst way to teach programming... just droning on while writing lines of code without any visual examples to complement the information or show why the engine demands the use of classes and constructors. It's an extremely lazy way to teach and very confusing for beginners.
sorry to tell you this but you talking to fast
Sarcasm?
homeschool mom and teenager. 💚✅
good for you girl