A Few Things That Every Unity Developer Should Know (Code Review)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here we're getting to our first actual warrior first thing to really think about and that would be our good friend here well actually no we'll do one thing first which is right now that little green line on the if statement before we get to that one the if statement the green line this isn't mandatory but it's another bit of good practice right that's going to tell you you can invert it that's what that green line will say so if you if you go over to that if and expand it out it will tell you invert if and it does this now what this does is it tells you that you had an if statement where there wasn't the else portion so what you could do is it turns into a guard clause you're basically saying I can take this and I can just exit out because nothing else runs past this point if this is not true or in this case if it's the opposite of what you're doing if you shouldn't dissemble then ignore don't do anything else beyond this point and that's just a bit cleaner it lets you exit sooner and you don't end up with what's called cyclomatic complexity and that's where you have multiple brackets deep and all of a sudden you have to mentally kind of zigzag your way through your code so if you if you exit early you can work on cleaner code step by step you know yeah so yeah this card Clause is great I mean obviously you want to make sure so for me the having the guard clause in the up in the update method I always feel a little bit like well the update method you might you might update it later huh you might change it later and so the update method could do so much more that you you can't predict because it's so generic so I always like kind of I don't want say shy away but it always makes it always feel like a code smell to me when I see it when I when I put a guard clause into an update but I mean I think in this case well I fun fact actually brings it up I do something about that myself which is whenever I have a guard cause what I do is I wrap everything else in the update and I give it a name so I would in this case call this update assembling or or I put it and then you then you have a guard clause and then the function that's called and that way you could very clearly see what would happen in the case of it hitting past the guard Club got it cool alright so we'll probably end up doing that but I'm not let's see what this thing does well you wanted to talk about this and I think that's yeah this is this is the first one that everyone should really be aware of right and I say this a lot because I think we need to make a video on this especially I don't think people fully understand what a component model is and how it works and that basically means a lot of stuff is happening magically by unity to kind of do things for you least of all the update function we haven't even gotten to magic functions yet I don't want to get into that right now but this call in particular is well worth knowing because people don't think about it but what that getcomponent does like I said imagine you had a stack of books and you want to get a certain book and someone said hey would you hand me that book and then the minute they say well they'll say do you have this book and he would then search the list get the book and say yes I have this book then you put it back in the stack and then someone says can I add that book you would they go through it again find the book and hand it to them you're doing the same thing twice arbitrarily that's costing extra cycles so whenever you call it get component you want to store the value of that so you can get back at it easier so you would remember where it is and you can refer to it so if you ever find get component inside of an update loop what you're basically saying is every time update is called walk through all the objects and then every time updates called walk through the objects again as some people might say there's only one or two components on this object it's not that much of a walk well the one or two components now and somebody might add more later and also if you've got ten items and they're all doing a walk of only two steps that's still 20 steps you know like you're you're massively scaling that as you go so get component cache them in your wake function let's do it let's catch this baby and we'll Charles is doing that I'll point out why I set awake function here's another interesting point based on the application model they're using a state system a life cycle that's very similar to say Android and so what I mean by that is every single script starts with there's a series of steps and there's a week and their start and then there's no them I don't go to all of them but the general point is awake happens then start happens and update happens and a lot of people don't realize is a wake and start happen once for the lifetime of that object so if we had a wake start on enable update on disable no matter if you toggle that object on and off what happens is the start will run once the awake will run once and the enable will toggle on and off so it's caching the data for it and then there's using the data as you go forward so what you want to do is anytime you want to use objects like this with get components you do it in awake and the reason you doing awake instead of start every single weight called in your entire application runs before the first start call so if you've got 50 scripts and you don't know which things will initialize first and which variables will be set what you want to do is you want to make sure that all the awake calls run so all of your data is cached and then the next thing the execute will be the start function so do all of the the logical bindings in a wake do all of your configuration and start
Info
Channel: Infallible Code
Views: 13,242
Rating: undefined out of 5
Keywords: unity, unity 3d, unity3d, game development, game dev, gamedev, unity code, unity coding, coding, programming, development, game programming
Id: 0vJLk8HTP8g
Channel Id: undefined
Length: 5min 12sec (312 seconds)
Published: Tue Dec 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.