C# Classes in Unity! - Beginner Scripting Tutorial

Поделиться
HTML-код
  • Опубликовано: 7 ноя 2024

Комментарии • 88

  • @leonardofraga5130
    @leonardofraga5130 5 лет назад +204

    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 года назад +2

      Well said ... thanks for the advice ♥

    • @proyas21
      @proyas21 3 года назад +1

      wow man. nice

    • @cabbagemontage6999
      @cabbagemontage6999 3 года назад +1

      Really good advice!

    • @gaussminigun7095
      @gaussminigun7095 3 года назад +4

      thanks dad

    • @coldshoulder14
      @coldshoulder14 3 года назад +1

      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 лет назад +26

    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 лет назад +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.

  • @WillRicketts
    @WillRicketts 3 года назад +39

    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 года назад +1

      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 Год назад +7

      @@mrhandexists20 the video title is “Beginner scripting tutorial”.

  • @BlackJar72
    @BlackJar72 4 года назад +31

    Uses an inner class; doesn't mention what he's doing or his rationale for doing so.

    • @quentincomacle
      @quentincomacle 3 года назад +9

      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 ?

  • @Gomace
    @Gomace Год назад

    So it's a bit like how Instantiate has different parameters based on what variables you give it? That's cool!

  • @Skyvastern
    @Skyvastern 5 лет назад +13

    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 лет назад +11

      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 лет назад +4

      @@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 лет назад +2

      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?

  • @Simon-Alexis
    @Simon-Alexis 3 года назад +4

    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 3 года назад +1

      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.

  • @Winnders
    @Winnders 4 года назад +3

    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.

  • @magnusm4
    @magnusm4 4 года назад +3

    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 года назад +1

      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

  • @ChauNezuko
    @ChauNezuko 3 года назад +2

    Wow, one of the best lecture
    /tutorial I ever watch, some confusing things are solved immediately. Thank you very much.

  • @compsciaaa9413
    @compsciaaa9413 2 года назад

    i cannot blieve that a unity tutorial has ended up making oop make sense for me haha

  • @Optimusprime-ve7xi
    @Optimusprime-ve7xi Год назад

    I have no idea how to start that so im gonna watch more

  • @JustOneHappyBoiii
    @JustOneHappyBoiii Год назад +1

    "Shoot small objects"
    *yeets a giant watermelon*

  • @perspektiva360
    @perspektiva360 5 лет назад +9

    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 года назад +1

      Was hoping to see this explained in the comments as well

    • @35matinee
      @35matinee 4 года назад

      If it's so well explained, why are you asking a question?

    • @oreore2208
      @oreore2208 4 года назад +3

      @@35matinee courtesy

  • @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.

  • @lakshmichandru3244
    @lakshmichandru3244 4 года назад +2

    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

  • @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?

  • @kodaxmax
    @kodaxmax 3 года назад +2

    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.

  • @mr_abex7579
    @mr_abex7579 2 года назад

    what should I do if unity don't automatically put public classes in the script for me?

  • @RegenerationOfficial
    @RegenerationOfficial 4 года назад +1

    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...

  • @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.

  • @animationjourney7207
    @animationjourney7207 3 года назад +3

    I see you are fan of "SPACE" and not "TAB" ///(

  • @Toxicblackmambahs
    @Toxicblackmambahs 4 года назад +1

    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.

  • @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.

  • @reys9913
    @reys9913 4 года назад

    Do we really need the third constructor ?
    The one below the //constructor comment
    5:46

    • @FlexFoundry
      @FlexFoundry 4 года назад +1

      I think so, because it gives the variables a default value instead of NULL.

  • @GianniPapetti
    @GianniPapetti 5 лет назад +1

    Why not just doing without class? is the same thing

    • @gregoryfenn1462
      @gregoryfenn1462 5 лет назад +1

      huh?

    • @GianniPapetti
      @GianniPapetti 5 лет назад

      @@gregoryfenn1462 I don't get the thing to make class in another class

    • @Toxicblackmambahs
      @Toxicblackmambahs 4 года назад +1

      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.

  • @MEGA_TREE
    @MEGA_TREE 2 года назад

    Very good

  • @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!?!?!?!?

  • @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

  • @THE_ONLY_GOD
    @THE_ONLY_GOD 2 года назад

    Not to be nit picky, but those aren’t brackets

  • @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 ?

  • @joshuatownsend1075
    @joshuatownsend1075 4 года назад

    Thanks.

  • @Marcusvcbr
    @Marcusvcbr 4 года назад +10

    I didnt understand, and dont make sense.

  • @alperen_101
    @alperen_101 3 года назад

    01:05 önemli

  • @gameginger5529
    @gameginger5529 3 года назад +1

    cofusing

  • @snailman3957
    @snailman3957 3 года назад

    small brain go brrr

  • @gaussminigun7095
    @gaussminigun7095 3 года назад

    english please

  • @adamdecoder1
    @adamdecoder1 2 месяца назад

    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.

  • @TimeLords910
    @TimeLords910 4 года назад +1

    sorry to tell you this but you talking to fast

  • @anaislake
    @anaislake 5 лет назад

    homeschool mom and teenager. 💚✅

    • @oguz8153
      @oguz8153 4 года назад

      good for you girl