I didn't use C# Interfaces for a very long time as I never saw value in them. But then one day I had to implement an interaction system and Interfaces were the answer I was looking for.
I will teach you Unity 1 on 1 https://www.tomiczengineering.com/mentorship
Twitter: https://twitter.com/DarkoTo56635877
Discrod: https://discord.gg/4CqnGESuRn
Github: https://github.com/tomicz
#unity #tutorials #practical #update #loop #unity3d #darko #tomic #programming #csharp #darko #tutorial #you #are #using #wrong
What is an update loop(0:50)
Stop checking every frame (1:33)
Don't call GetComponent in update loop(5:54)
Coroutine vs update loop(6:15)
Remove empty loops(6:44)
Stop using update loop all the time(7:08)
One more optimization tip(7:59)
Join Discord community(8:52)
you have an idea for this game you watch some
tutorials added some enemies created a player character you have the latest graphics card
for the Knight DTI but for some reason your game runs slowly at only 20 FPS then you start
having these self-doubting thoughts because you just realized that a full fledged game like
Red Dead Redemption 2 runs at steady 60fps on a microwave while your empty project that
contains only placeholder white cubes burns more electricity than a Bitcoin Farm if you
can relate to this then watch this video till the end and I will tell you exactly what you
are doing wrong it's the same reason why all those Early Access games on Steam are always
laggy it's because their developers didn't subscribe to my channel and if you also didn't
subscribe please do it now let's begin our video you are already probably familiar with an update
loop it's one of those first things that you will ever learn in unity an update Loop is only
executed once per frame but what
does that mean if your game is running at 60 FPS that means that
an update Loop will execute exactly 60 times if you tell Unity to print hello world in an update
Loop it will print it 60 times every second so this is only a simplified way of how it works I
assume that many of you have used an update look before but what if you are unaware that you are
doing something wrong in that case I'm going to guide you through some points of what you could be
doing wrong and provide you with some alt
ernatives recently A friend of mine reached out to me to
help him with his code he showed me this class of enemy where he checks whether enemy is dead or not
if it's true then it proceeds to destroy the enemy game object if it's not true then it returns out
of the method the only problem with his approach is that he is doing all of this in an update Loop
so that means that his CPU will keep checking the enemy State forever is this really necessary in
a real case scenario you would want to k
now the enemy state only when you need to know it let
me explain since his game is running at 60 FPS that means that it will check enemy State 60
times every second so your computer is doing all these extra calls that the player does not
benefit from a player does not need to know if enemy is dead or not every frame every second on
top of that it is wasting CPU resources that could be allocated somewhere else in other words this
is what we call game optimization you have to be very smart wi
th how you did distribute tasks for
your computer but remember that my friend had 10 enemies which means that his CPU had to check
for 10 enemies and not for one for 10 enemies that is 60 times 10 equals 600 checks every second
that's still okay if you are running on a high-end PC but let's say that you are making a game for
Android phones and it needs to run on a low end Android phones that could drastically affect its
performance and even though your high-end PC can eat all of this it doe
sn't mean that you should
keep doing this bad practice or on a job interview an interviewer might dismiss you immediately if he
notices that you are doing this beginner's mistake also my friend had this code where he finds the
distance between the player and the enemy which was like 5 lines of code of course I'm just making
this up for this simplification now let's do the math 5 times 60 equals 300 calls and times 10
enemies which is 3 000 calls every second let's not forget from this other
example those 600 calls
which now adds up to 3600 calls every second and this is one of the most common mistakes that every
beginner does everything that they add to the game they put it inside an update Loop and not just
them but also in these other YouTube tutorials you can see it too that's exactly why you cannot
escape the tutorial hell is because you were never taught the best practices 3600 cores might not be
much but things can add up easily and now it could become 80 000 calls and
your game could experience
spikes which is something you never want to happen in an example where we check whether enemy is dead
or not instead we could do checks only when it's hit by a projector and now when projector Collies
with the Enemy that's the only time where we're going to check the enemy state so we don't need
to check enemy stay constantly like we did before at Max let's say we can only shoot 15 projectiles
in one second that will make a worst case scenario 15 and that will red
uce calls from 600 to 15 and
our best case scenario is zero or no calls at all because our enemy is not shooting and if player
does the shooting and misses all the enemies that's still going to be zero cause because
no enemy was collided with the projectile if you remember calls are only made when projectile
successfully collides with the Enemy so we could make our projectiles disable self on collision
and that will make our CPU to only make calls on a single enemy but in my friend's exampl
e it
made calls on every single enemy in the scene even those that are not in the player's view now
imagine a large game as a Grand Theft Auto and if Rockstar developers checked if some random NPC
at the other end of the map was alive and imagine if they did that for the every NPC in the game
and do you know how many NPCs are there in GTA probably thousands do the math and see yourself
how many calls are there of course this is just an exaggerated example of our problem Rockstar
developers
will never have some random NPC at the other end of the map loaded in the memory
normally in games like these an NPC would be loaded only when the player is close to him
and this is what we call object pooling and we will discuss that in the future videos never
call get component in an update Loop ever it's a relatively expensive operation it searches
through all game objects in the scene hierarchy until it finds the one that you need and it's
another good thing to do what you want to do i
nstead is to Cache the reference in the awake or
start or just drag the reference in the inspector another thing I want to add is to build a
habit of using core routines quarantines are way easier to handle than having the same
code running in an update loop it's way easier to stop according when it finishes while an
update Loop can only be disabled when the game object is disabled but what if you can't
disable that game object then that update will keep running I will make tutorial on core
routines so if you didn't subscribe please do it this is a very common beginner mistake and you are
probably unaware that you are doing it never leave an update Loop empty ever because Unity will
call that update Loop internally even though it's empty and it will make unnecessary calls is
the best practice to remove awake unenable start update fixed update all those methods if they
are empty I used to do this as well don't go for an update Loop immediately 99 the script
that you are writi
ng probably can be written outside of an update Loop if you are dealing
with the UI stuff like players Health enemies held remaining bullets anything that needs to
change constantly all of that can be updated in a single frame don't even think of an update
Loop what I recommend instead is to go for an event-driven Solutions and what do I mean by
event driven as a beginner I used to grab a player and grabs its Health property and then I
would update that Health constantly in an update Loop a
nd so the CPU would tell the player hey this
is your health even when the hell didn't change so it's very important to make calls only when
something actually changed and not every frame here's one more optimization tip that I will
share with you I have this skeletons AR game that's of course an augmented reality game in
order for a player to be able to play the game he has to place this turret on the ground so for
my phone to be able to place the turret on the ground he has to run this Ray
from the phone in
direction of the ground and the only way to do this it has to be run in an update Loop raycasts
are very expensive so you don't want them to run when not used so when I actually place the turret
on the ground I disable the game object that runs the raycast so the CPU is not calculating the
distance between the phone and the ground when the player is playing the game because it doesn't
benefit from knowing that information and also the phone battery will last longer becaus
e of that
in conclusion always disable game objects that run an update Loop if possible if you are looking
for free unity and programming tips I built the community that you can join I will provide
the link in the description it's still a new community that I'm looking to grow and I'm always
there so if you have any questions feel free to ask me this is it guys if this video was helpful
Please Subscribe and see you in the next video
Comments
During my early Unity years, I challenged myself to create modules without using "Update" except when you need to accept Inputs from the user. This is something that I still follow and have also taught this to many of the junior devs in my company. It's almost always part of my TODOs for writing any thing. Events are the best.
I've always done GetComponent just once in Awake() or Start() and assigning it to a variable within the object. Never thought of just dragging the reference in the Inspector, but good to know.
Join our discord community and get the best Unity tips https://discord.gg/4CqnGESuRn. And sorry for the audio.
Man, this video is just insane, thank you so much. I subscribed.
My friend, your audio only goes into my left ear.
The only thing you should do each frame is listen for player input. Everything else should be event based. Send out an event (I like Action/Func), and whatever system interested in knowing can subscribe to that event.
This was an awesome video! Can't wait for more, specifically on coroutines!
Svaka cast! Ovakvi detaljni tutorijali su nam potrebni! Kada sam poceo game development, moja prva prosta android igrica "Falling blocks" je jedva radila na telefonu upravo zbog lose optimizacije! Ali godinu dana kasnije, sa zavrsenom IT akademijom i dobrim znanjem C# jezika, sada svaka igrica radi u 60+ fps
Just the kind of content I was looking for!
I like your presentation. The only one I truly understand. Please keep these coming. Subscribed, and liked all of your posts.
This video feels like a marvel henchman is menacingly teaching me C# instead of interrogating me lol
Thank you ! your way of explaining is very soothing and easy ! THANKS
This is really great information.
thanks for the advice, very useful!
You have to be careful with OnTriggerEnter or OnCollisionEnter too. And don't forget about the collision filter. What can you say about UniTask? As an alternative to updates and coroutines? Suppose we run a UniTask loop with Delay Frame(120) or just Delay(seconds) So that the check does not take place every second, but only a certain number of times per minute?
When I really need to make check in update. I will always go for fixedUpdate and if I can go even lower I will create a slowUpdate I only call 4x per second for some AI check for example and when they are out of the camera bounds I disable them. No one notice the difference in game except the fps.
Man, please never leave youtube, my game fps gone from 15 fps to 144fps (monitor max fps), thanks 💯
Great video, I have subscribed.
i really dont like to put anything in update and i try avoiding the most but sometimes im just not sure how to update a value without a start point, but as you stated, events is good to go
Thanks for the video man.. (The sound was a little off coz your voice came only from the left speaker, just a notice) Waiting for more informative videos ;)