C# Classes in Unity! - Beginner Scripting Tutorial

preview_player
Показать описание
Watch this video in context on Unity Learn:

How to use Classes to store and organise your information, and how to create constructors to work with parts of your class.
Рекомендации по теме
Комментарии
Автор

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

leonardofraga
Автор

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.

jaypark
Автор

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.

WillRicketts
Автор

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.

Skyvastern
Автор

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

BlackJar
Автор

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

Gomace
Автор

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

compsciaaa
Автор

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

magnusm
Автор

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

ChauNezuko
Автор

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.

pianoatthirty
Автор

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.

Winnders
Автор

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

Optimusprime-vexi
Автор

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!)

Simon-Alexis
Автор

"Shoot small objects"
*yeets a giant watermelon*

JustOneHappyBoiii
Автор

I just came here to understand how to call classes dynamically and for example leave my recoil class unopened/unused and therefore the parameters in it set to 0 or alike when i call my laser class but i still want to include the recoil class when i shoot a gun or launch a missle. Somehow tickboxes appear in this Script, but i really don't know why

jonassackarndt
Автор

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

lakshmichandru
Автор

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?

perspektiva
Автор

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.

kodaxmax
Автор

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?

paulbelfrage
Автор

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