Monster shadow

VIDEO

C# Beginner Tips: Baby please, just the tips

Description:
Learning C# can be daunting, as with any new skill, But I'm here to help. It's okay to be a noob. We were all noobs at one stage.
ADD A COMMENT

Thomas Ingram
I feel like you cannot mention null coalescing operators without mentioning they they do not work properly with UnityEngine.Object types. Which is a major part of Unity development. The equality operators (== and !=) for UnityEngine.Object types have been overridden by Unity, and don't only perform reference comparisons, this makes it possible to Destroy an Object somewhere, and have an entirely different reference evaluate to null. What this also means is that these objects are not actually null, and attempting to use them will cause exceptions and various other issues. The null conditional (?.) null coalescing (??, ??=) operators, and is null checks (is null, is not null, is {}) will not function correctly, as Unity cannot override those operators.
IronLion
@Konitama 🤣
Konitama
Wish I read this before I went through like 20 scripts and changed all my if == null to ??= thinking I was making my code a bit neater... only to get nonstop game object not found errors.
Zak Kaioken 2
that just happens to be a case where you shouldn't be using a unity object at all. if you need that actual c# null just use a basic c# class instead of derriving unityengine.object.
Tarodev
You're right. I'll pin this as a disclaimer
Jeremiah T
4:12 I've often used this example for checking plurality. string result = amount > 1 ? "people" : "person";
Kuma
print is going to change my life
Andersen Castañeda
Nice video. The null coalescing operator is part of C# 8, so only will be available on Unity 2020 or newer.
Tarodev
Good thing to note
Roman Salnikov
I have been developing on Unity and C# for a year now, but most of these tips were new and useful to me. So bases on the title of this video I'm still a beginner...DDD
Dino Fejzagic
Never saw someone name namespaces using an underscore prefix. It hurts my eyeballs. Just kidding, great video!
Tarodev
@Dino Fejzagic Ahhhhh! Yes, that's just a byproduct of prefixing the scripts folder in the unity engine with an underscore. Certainly a gross one at that. I'd never purposefully do it ><
Dino Fejzagic
@Tarodev I was referring to the namespaces, namespace _Scripts { ... } in your examples. I know a lot of people use _ for private fields. I was using it for a long time as well but at some point stopped and I am still alive :D. I feel like the IDE gives me enough context to distinguish. Obviously this is all a matter of personal preference.
Tarodev
Do you mean the variables? It's actually proper c# convention. Only unity decided to discard any decent naming conventions and just wing it. Can you look at a variable in a function and know if its a local or private global, or do you use the same naming convention for both of those things? 😉
djblast101
The title is why I clicked 😂
Frank Lin
Thanks.. Animation mask tutoriel please
AB RAIYAN
Can we get a long video on Events and Delegates, please?
AB RAIYAN
@Tarodev Thanks a lot dude. I really wanna see your channel grow.
Tarodev
I'd say one of the next 3 videos will be this topic :)
Milly Chips
the editing kept my attention on the video, thanks for the silliness
Tarodev
Too many professional tutorials out there, I think...
Anders Nielsen
Since Brackeys shut down, you are my favorite source of Unity tips. Thank you.
Rafael Pontes
Hey, quick question: what is the size of a "Hello World" app made with Unity? Does C# introduce a big core bundle overhead to the exported binary?
Rafael Pontes
@Tarodev, interesting!! Thanks for the reply! =)
Tarodev
Unity builds can be as little as 15mb (2mb using a special sub-version called project tiny).
supendi Xu
Why does this guy only have 9k subscribers 😭
o o̶
👍🏻
Eadlef
instance ??= this; The gourmet singleton
Tarodev
That is beautiful
TrashCaster
The "set override" was neat to me. I come from a predominantly Java background, so learning new C# things is nice for me, and gives me more reasons to like it over Java. Being able to effectively bind a value to a string is super handy. No more calling update/refresh functions.
QueenOf Pain
I personally don't (or rarely) use the else statement for anything. It makes my code easier to understand (for me) and much cleaner with less brackets. I also don't use the switch statement at all. Instead, I just do a bunch of "if() return;"s. I don't know if there's any performance impact on my choices but like you mentioned it's just my personal preference. :) Love your videos
Tomáš Petrlík
As a professional programmer I can tell you that even in most companies they recommend you to not use else branch if possible.
Roboapple
is there a video that explains the use of the "using" keyword? I didnt know it had uses outside of calling for namespaces/statics at the top of you script. I tried reading the docs page for it but i didnt really understand it.
Tarodev
It's a special keyword specifically for the Idisposable interface. It allows you to encase some logic which uses the disposible class, giving you granular control for when to call dispose.
g0nz0
Me before vid: Beginner! Me After vid: "Beginner"
Tarodev
Heh 😊
A D
Great tips, I knew most of them but things like string builder and the print are good to know . This would have been so valuable to have when I was starting out so good job
SEE ALL COMMENTS


Transcript:

okay so this first one isn't exactly a code tip but uh i'm going to be using
this through this video so i figured i'd show you it if you check my community
page you've probably already seen me post this but it's just a custom class
that i'm calling watch and it's implementing our disposable and what
this gives us is when the class goes out of scope or we stop using it uh dispose
method is automatically called for us uh basically what you would normally use
this for is to clean up some managed resources something that the garbage
collector doesn't automatically do for you
but for our case when we create this watch we're just starting a new
stopwatch and then when it's disposed we're stopping the watch and we're
printing out the elapsed time and the way we can use this is
just with the using statement uh new watch actually you don't even need that
you can just do that new watch uh and basically here this could be any task
that you want to time right now i'm just setting a delay of uh two and a half
seconds and then it should print out the results so let's go across to unity
press play it's just going to wait two and a half seconds
and example 2500 because that's how many milliseconds it
took so that's that's so moving on to the actual tips now
okay so this first one uh is something that you probably never thought about
and that is the order that you put your if conditional
statements in so for example i've got these two tasks here they both return a
bull this one has a little 2.5 second weight and then returns false this one
returns false immediately now if you were going to write a conditional here
an if statement what you need to understand is that
these statements are processed from left to right so if you put this long ball
here first followed by the short ball
it's going to process this one first take two and a half seconds and then
decide nope it's returned false so then we'll just skip this right so what you
should actually do is put your cheap and fast running calls
first and then leave your more long expensive calls to the end a direct
example of that would be for example if you had a function which would which had
to do a raycast or a bunch of raycasts uh to check if you're grounded or if
this condition or whatever is met you should put that after simply checking
input is keyed down right put the super fast cheap calls first then put the
expensive calls after it okay and then it will it'll give you a tiny little uh
increase in performance okay in this one i've got some text mesh
pro just on the ui i've got a value here called some value and in this function
i'm altering the value and then i am setting the uh i'm updating the text to
display this new value and let's assume that some value is being edited in a few
places could be in this script could be in another script
probably not the best design but just for the example it's being edited in a
few places which means you're you're putting this
in a few places as well technically but ultimately this text should be
tightly coupled to this value you never want this text to not be exactly what
the value is so instead of setting the text here directly like and then also in
all the other functions that you're doing it you can remove that and then in
your setter instead of just setting the value you can also set the text so then
you're coupling them together it's always going to be exactly what some
value is worth so yeah it seems like a pretty
obvious tip but i bet you it's going to help a few of you out
okay this next one is very simple it's more of like a personal preference for
me uh so say you've got a logic gate here and it's just checking if you're
pressing the w key and if so uh let's move the character or whatever then you
may have another logic get in here so then you're nesting more stuff uh just
gets a little bit messy so this is personal preference but i much prefer to
invert the boolean and simply return out of it
so i personally think that this is much cleaner than uh nesting and nesting and
nesting and i think it's easier to read if they're not pressing the w key this
does not concern them pretty pretty simple
yeah there's no performance pros or cons here either way it's just personal
preference so uh use it if you prefer it okay this next one's about the ternary
operator so you've got some kind of string or value or something and you've
got a conditional where you're going to set a value
instead of writing it like this you can simply
restructure it and actually we can just remove that completely and make that the
variable so now on a single line you've got uh
you're doing your conditional here and you're saying if it's true then set it
to this otherwise set it to this so it's just an if else statement one nice
single line and yeah that's that one okay so this next one is the null
coalescing operator so say you've got some value here you don't know if it's
going to be null or not and you need to assign a value to this new variable that
you've got basically you can just say all right i would like it to be this
but if it's null with this little coalescing operator
set it to this default value or some kind of fallback value so this is the
same as saying uh if if this is not null then use some value otherwise use
default value so just a nice little one liner
alright so this next one is about uh building up your strings so it's not so
relevant in game dev but in uh just development in general uh string is
an immutable type so when you are when you have your string here and then you
are concatenating on a new string what this is doing is it's actually
generating an entire new string uh because as i said it's immutable it
can't just add this onto the end of this string all right gotta make a whole new
string every single time so we would create 10 000 new strings
by the end of of this before we print it what you can do is you can create a
string builder and append the
new pieces of string and then at the end you can just to string it and the
performance difference between these two is quite massive
let me show you so
with concatenation it is 1.5 seconds where the string building it is just 15
milliseconds so as you can see the benefits are
massive of using a string builder uh saying that though if you're just
concatenating like two or three times don't worry about it uh but as soon as
you start getting like around the ten concatenations i would start using a
string builder and just this last one if you hadn't already picked it up uh
through my last examples instead of doing debug log you can simply do print
as long as you're inside a mono behavior because if you decompile print and have
a look you'll just see that uh it just it's just a wrapper for debug log
so just a little bit nicer to type instead of that
and that's it i know there were very basic tips but hopefully i taught you
something uh subscribe like and i'll see you in the next video bye
[Music] [Music]
you