C# Classes in Unity! - Beginner Scripting Tutorial

  Рет қаралды 128,978

Unity

Unity

Күн бұрын

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
@leonardofraga5130
@leonardofraga5130 5 жыл бұрын
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...)
@mehdidev9926
@mehdidev9926 4 жыл бұрын
Well said ... thanks for the advice ♥
@proyas21
@proyas21 3 жыл бұрын
wow man. nice
@cabbagemontage6999
@cabbagemontage6999 3 жыл бұрын
Really good advice!
@gaussminigun7095
@gaussminigun7095 3 жыл бұрын
thanks dad
@coldshoulder14
@coldshoulder14 3 жыл бұрын
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
@jaypark2177
@jaypark2177 5 жыл бұрын
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.
@xanaramus
@xanaramus 5 жыл бұрын
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.
@St4rdog
@St4rdog 5 жыл бұрын
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.
@BlackJar72
@BlackJar72 4 жыл бұрын
Uses an inner class; doesn't mention what he's doing or his rationale for doing so.
@quentincomacle
@quentincomacle 3 жыл бұрын
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 ?
@WillRicketts
@WillRicketts 3 жыл бұрын
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.
@mrhandexists20
@mrhandexists20 2 жыл бұрын
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
@PaulJonesy Жыл бұрын
@@mrhandexists20 the video title is “Beginner scripting tutorial”.
@Skyvastern
@Skyvastern 5 жыл бұрын
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.
@leonardofraga5130
@leonardofraga5130 5 жыл бұрын
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.
@Skyvastern
@Skyvastern 5 жыл бұрын
@@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
@dtr0q10
@dtr0q10 5 жыл бұрын
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.
@mohandasjung
@mohandasjung 2 жыл бұрын
@@dtr0q10 So... did they implement it?
@Winnders
@Winnders 4 жыл бұрын
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-Alexis
@Simon-Alexis 3 жыл бұрын
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!)
@slash148
@slash148 2 жыл бұрын
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.
@magnusm4
@magnusm4 4 жыл бұрын
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
@pebblessyou
@pebblessyou 4 жыл бұрын
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
@Gomace Жыл бұрын
So it's a bit like how Instantiate has different parameters based on what variables you give it? That's cool!
@lakshmichandru3244
@lakshmichandru3244 4 жыл бұрын
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_abex7579
@mr_abex7579 2 жыл бұрын
what should I do if unity don't automatically put public classes in the script for me?
@perspektiva360
@perspektiva360 5 жыл бұрын
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?
@jeffreyhicks6380
@jeffreyhicks6380 4 жыл бұрын
Was hoping to see this explained in the comments as well
@35matinee
@35matinee 3 жыл бұрын
If it's so well explained, why are you asking a question?
@oreore2208
@oreore2208 3 жыл бұрын
@@35matinee courtesy
@beeyakortah
@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
@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.
@Gregorz
@Gregorz 3 жыл бұрын
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.
@sahilvishwakarma6509
@sahilvishwakarma6509 3 жыл бұрын
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.
@paulbelfrage
@paulbelfrage 4 жыл бұрын
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
@JustOneHappyBoiii Жыл бұрын
"Shoot small objects" *yeets a giant watermelon*
@RegenerationOfficial
@RegenerationOfficial 4 жыл бұрын
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?
@giammariomarmorino50
@giammariomarmorino50 3 жыл бұрын
float x = GetComponentInChild(); stats.attack/defense/etc...etc...
@compsciaaa9413
@compsciaaa9413 Жыл бұрын
i cannot blieve that a unity tutorial has ended up making oop make sense for me haha
@ChauNezuko
@ChauNezuko 3 жыл бұрын
Wow, one of the best lecture /tutorial I ever watch, some confusing things are solved immediately. Thank you very much.
@kodaxmax
@kodaxmax 3 жыл бұрын
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
@Optimusprime-ve7xi Жыл бұрын
I have no idea how to start that so im gonna watch more
@reys9913
@reys9913 4 жыл бұрын
Do we really need the third constructor ? The one below the //constructor comment 5:46
@FlexFoundry
@FlexFoundry 4 жыл бұрын
I think so, because it gives the variables a default value instead of NULL.
@pianoatthirty
@pianoatthirty 2 жыл бұрын
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.
@animationjourney7207
@animationjourney7207 3 жыл бұрын
I see you are fan of "SPACE" and not "TAB" ///(
@GianniPapetti
@GianniPapetti 5 жыл бұрын
Why not just doing without class? is the same thing
@gregoryfenn1462
@gregoryfenn1462 5 жыл бұрын
huh?
@GianniPapetti
@GianniPapetti 5 жыл бұрын
@@gregoryfenn1462 I don't get the thing to make class in another class
@Toxicblackmambahs
@Toxicblackmambahs 4 жыл бұрын
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.
@Toxicblackmambahs
@Toxicblackmambahs 4 жыл бұрын
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.
@Jizu16
@Jizu16 3 жыл бұрын
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 ?
@timothygooding9544
@timothygooding9544 3 жыл бұрын
any way to get communication across scripts?
@unity
@unity 3 жыл бұрын
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/
@timothygooding9544
@timothygooding9544 3 жыл бұрын
@@unity thank you so much
@unity
@unity 3 жыл бұрын
@@timothygooding9544 Happy to help!
@crassianleviathan6187
@crassianleviathan6187 3 жыл бұрын
Why is it light mode!?!?!?!?
@THE_ONLY_GOD
@THE_ONLY_GOD 2 жыл бұрын
Not to be nit picky, but those aren’t brackets
@MEGA_TREE
@MEGA_TREE 2 жыл бұрын
Very good
@mikevick707
@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
@joshuatownsend1075
@joshuatownsend1075 3 жыл бұрын
Thanks.
@Marcusvcbr
@Marcusvcbr 4 жыл бұрын
I didnt understand, and dont make sense.
@alperen_101
@alperen_101 3 жыл бұрын
01:05 önemli
@snailman3957
@snailman3957 3 жыл бұрын
small brain go brrr
@adamdecoder1
@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.
@gameginger5529
@gameginger5529 3 жыл бұрын
cofusing
@gaussminigun7095
@gaussminigun7095 3 жыл бұрын
english please
@TimeLords910
@TimeLords910 4 жыл бұрын
sorry to tell you this but you talking to fast
@jasdrawssometimes4660
@jasdrawssometimes4660 4 жыл бұрын
Sarcasm?
@anaislake
@anaislake 5 жыл бұрын
homeschool mom and teenager. 💚✅
@oguz8153
@oguz8153 4 жыл бұрын
good for you girl
How to Program in C# - Classes (E07)
19:41
Brackeys
Рет қаралды 421 М.
Миллионер | 2 - серия
16:04
Million Show
Рет қаралды 1,3 МЛН
А что бы ты сделал? @LimbLossBoss
00:17
История одного вокалиста
Рет қаралды 9 МЛН
C# Loops in Unity! - Beginner Scripting Tutorial
6:32
Unity
Рет қаралды 121 М.
C# Vector Maths in Unity! - Beginner Scripting Tutorial
9:10
JavaScript being split?? JS0 and JSSugar
ThePrimeTime
Рет қаралды 1,1 М.
C# DataTypes in Unity! - Beginner Scripting Tutorial
4:50
C# Enumerations in Unity! - Beginner Scripting Tutorial
4:27
Миллионер | 2 - серия
16:04
Million Show
Рет қаралды 1,3 МЛН