Watch this video in context on Unity Learn: learn.unity.co... How to use Classes to store and organise your information, and how to create constructors to work with parts of your class.
Пікірлер: 88
@leonardofraga51305 жыл бұрын
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...)
@mehdidev99264 жыл бұрын
Well said ... thanks for the advice ♥
@proyas213 жыл бұрын
wow man. nice
@cabbagemontage69993 жыл бұрын
Really good advice!
@gaussminigun70953 жыл бұрын
thanks dad
@coldshoulder143 жыл бұрын
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
@jaypark21775 жыл бұрын
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.
@xanaramus5 жыл бұрын
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.
@St4rdog5 жыл бұрын
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.
@BlackJar724 жыл бұрын
Uses an inner class; doesn't mention what he's doing or his rationale for doing so.
@quentincomacle3 жыл бұрын
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 ?
@WillRicketts3 жыл бұрын
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.
@mrhandexists202 жыл бұрын
Thats cool but this is a part of a series and he even says its intermediate so a beginner really shouldnt even be here.
@PaulJonesy Жыл бұрын
@@mrhandexists20 the video title is “Beginner scripting tutorial”.
@Skyvastern5 жыл бұрын
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.
@leonardofraga51305 жыл бұрын
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.
@Skyvastern5 жыл бұрын
@@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
@dtr0q105 жыл бұрын
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.
@mohandasjung2 жыл бұрын
@@dtr0q10 So... did they implement it?
@Winnders4 жыл бұрын
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.
@Simon-Alexis3 жыл бұрын
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!)
@slash1482 жыл бұрын
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.
@magnusm44 жыл бұрын
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
@pebblessyou4 жыл бұрын
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
@Gomace Жыл бұрын
So it's a bit like how Instantiate has different parameters based on what variables you give it? That's cool!
@lakshmichandru32444 жыл бұрын
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
@mr_abex75792 жыл бұрын
what should I do if unity don't automatically put public classes in the script for me?
@perspektiva3605 жыл бұрын
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?
@jeffreyhicks63804 жыл бұрын
Was hoping to see this explained in the comments as well
@35matinee3 жыл бұрын
If it's so well explained, why are you asking a question?
@oreore22083 жыл бұрын
@@35matinee courtesy
@beeyakortah Жыл бұрын
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.
@MrCaturday Жыл бұрын
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.
@Gregorz3 жыл бұрын
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.
@sahilvishwakarma65093 жыл бұрын
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.
@paulbelfrage4 жыл бұрын
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?
@JustOneHappyBoiii Жыл бұрын
"Shoot small objects" *yeets a giant watermelon*
@RegenerationOfficial4 жыл бұрын
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?
@giammariomarmorino503 жыл бұрын
float x = GetComponentInChild(); stats.attack/defense/etc...etc...
@compsciaaa9413 Жыл бұрын
i cannot blieve that a unity tutorial has ended up making oop make sense for me haha
@ChauNezuko3 жыл бұрын
Wow, one of the best lecture /tutorial I ever watch, some confusing things are solved immediately. Thank you very much.
@kodaxmax3 жыл бұрын
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.
@Optimusprime-ve7xi Жыл бұрын
I have no idea how to start that so im gonna watch more
@reys99134 жыл бұрын
Do we really need the third constructor ? The one below the //constructor comment 5:46
@FlexFoundry4 жыл бұрын
I think so, because it gives the variables a default value instead of NULL.
@pianoatthirty2 жыл бұрын
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.
@animationjourney72073 жыл бұрын
I see you are fan of "SPACE" and not "TAB" ///(
@GianniPapetti5 жыл бұрын
Why not just doing without class? is the same thing
@gregoryfenn14625 жыл бұрын
huh?
@GianniPapetti5 жыл бұрын
@@gregoryfenn1462 I don't get the thing to make class in another class
@Toxicblackmambahs4 жыл бұрын
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.
@Toxicblackmambahs4 жыл бұрын
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.
@Jizu163 жыл бұрын
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 ?
@timothygooding95443 жыл бұрын
any way to get communication across scripts?
@unity3 жыл бұрын
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/
@timothygooding95443 жыл бұрын
@@unity thank you so much
@unity3 жыл бұрын
@@timothygooding9544 Happy to help!
@crassianleviathan61873 жыл бұрын
Why is it light mode!?!?!?!?
@THE_ONLY_GOD2 жыл бұрын
Not to be nit picky, but those aren’t brackets
@MEGA_TREE2 жыл бұрын
Very good
@mikevick707 Жыл бұрын
"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
@joshuatownsend10753 жыл бұрын
Thanks.
@Marcusvcbr4 жыл бұрын
I didnt understand, and dont make sense.
@alperen_1013 жыл бұрын
01:05 önemli
@snailman39573 жыл бұрын
small brain go brrr
@adamdecoder1Ай бұрын
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.