10 Things You NEED to Be Doing in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Always helpful, code daddy.

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/holtzzy123 πŸ“…οΈŽ︎ Nov 11 2022 πŸ—«︎ replies

You should get a ring light, or even just a lamp, if you’re gonna make more vids. Especially since your lit up stuff in the background makes your face even more dark by contrast.

I like this video. I had no idea you can instantiate a prefab with only a reference to one of its components. Of course I’m never gonna do that cause it’s confusing but I was like β€œhuh. No kiddin.”

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/SirWigglesVonWoogly πŸ“…οΈŽ︎ Nov 11 2022 πŸ—«︎ replies

Very helpful tips...

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/jubin_jajoria πŸ“…οΈŽ︎ Nov 11 2022 πŸ—«︎ replies
Captions
hey friends I'm here to teach you some hot tips most of these are for beginners but if you're a veteran stick around because I may still teach you something and the first one is serializing your prefabs as game object now I see so many new devs doing this for example here I've got a carrot prefab that I'm serializing as game object and here I'm instantiating it but then I'm immediately using get component carrot on it so that I can call whatever method is on the carrot that I need instead of doing this simply serialize what you need from from the get-go so you just serialized carrot and you can do away with that now on top of that say you have got a prefab can be anything so let's have a look at my prefabs here so for the carrot for example I've got all these components here say I need to spawn this and then I just need to play a sound from its audio Source I could actually serialize this as an audio source uh obviously that's not going to work there but on my carrot spawner I could quite easily still serialize this object here in my audio Source component right and I can still instantiate it just like normal and now I've got access to the audio source so this would be carrot you know I could do a play on it so always serialize what you need as you need it not just as game object and just another tip while we're here on drug gizmos as you can see I'm drawing a gizmo at my spawn point so I know where to spawn them so in my scene I can actually say that Gizmo right here and if I move around my spawn point I can say that that moves around so it's an easy way to keep track of things in the same all right so this one may seem pretty damn obvious but only use awake to initialize the actual object itself so for example I'm just using a static instance for this example but this could be anything right this could be like an audio manager an ik manager that needs specific actions to take place before other scripts can start to call it it needs to set itself up so in this scenario I am setting myself up and creating the instance and then I've got a function here called do something I've got another static instance over here which is doing the same thing but it's calling this static instance here from start now if we were to do this in a wake there could be a race condition here and this would not have initialized itself before calling so rule of thumb is never call other scripts in awake only initialize yourself do your own setup in awake and then from start you should be safe to start calling other Scripts all right I dare say this is going to come as a bit of a shock to most devs but you should never be using a public field there's there's really never a good use case for it so if your sole purpose is you would just like to serialize this in the editor make it private and serialize it like this the the name of the game is locking down your scripts as much as possible even if you're the only one working on the code base only expose things to other scripts that you need exposed right you don't want others if I made this public that this carrot prefab for example another script could come in and just assign a completely different prefab to this variable right or even null it um obviously you'd be kicking yourself in the foot but the whole point is to just lock everything down but say you don't want to serialize this in the editor that's not what you care about uh you just want this to be accessed externally in that case what you should do is create a a getter right so this will make it Reader only to external scripts but as we're changing it in this script we can make it a private setup and now this is like a public field but it's more locked down it's only read only now there has been a downside until just recently Unity does not serialize or has not serialized properties in the past but they do now so all you need to do is add a serialized field just prefix it with field and this will serialize in the inspector there is one other way around it just say I want to serialize this for example and I don't want to change it to a property because I don't want to break the link with the editor right it could have been a whole bunch of uh a whole bunch of stats or settings that I've set up what you can do is create a public and it's going to be carrot in my case carrot prefab and I'm just going to return the carrot prefer so this here is just shorthand for this we're basically just making a public getter for it so that will work perfectly fine other scripts will be able to grab that all right so this next one is just about iterating through a collection and once you get to the end of the collection you're resetting the index back right this is a very common pattern I'm sure all of you do it and I just want to show you a sexier way to do it which is to use the modular operator so you still need to click keep your clip index or whatever index you're using uh you iterate it and then you're just using modulator to the clips length and it will just wrap over and over and it will never overflow so you're really only saving one line but I use this all over the place so you know uh it's just a nicer way to do it okay this next one is more for rapid prototyping so I don't know about you but I'm prototyping all the time and I don't want to set up all my systems uh properly from scratch I don't even know if I'm going to continue with the idea you know so say I did not serialize this audio Source here um I didn't actually even have have it have an audio Source on my on my character what you can actually do is you can say audio source and use the static method play clip at point and this will take in the clip position and volume so you can it's 3D sound still and you don't need an audio Source anywhere in your scene for this to work uh super super easy to use and saves me a bunch of time and to be honest it's not just for prototyping if you're not using an audio mixer in your project right it's like a mobile project or something this could perfectly work in production mode all right so every time you change a transform you need to make an extern call which while the unity has done it for you but the extern call basically means that it is contacting the CP C plus plus binaries and doing the code there and although tiny there is a slight overhead there for example Rider right now is suggesting that I introduce a variable because even when reading the transform you're making an external call there as well so this just caches it but that is not actually the tip that I am going to tell you right now I'm saying position and rotation in two lines which is two separate extern calls what you can do is set position and rotation now for some reason I did not discover this for a long time but it's very easy to set them both with one extern call saving you just a slight bit of overhead if you're doing this every single update frame with multiple objects You Know It uh may make a difference plus it's kind of just nice having it all on one line all right so this one is awesome and I use it all the time so I've got a stat struct here it's got attack power and health so say you've got a bunch of scriptable objects of units and each unit has a base stat which never changes right it's the base stat of that unit once the player selects a unit and they're leveling up that unit they're putting in stat powers and skill points and stuff so then they've got these extra stat points that need to be combined with the base stat points so when you start a level you can bind them then you've got the final stats traditionally you would do something like this you got your base stats your leveled stats and then you just combine them like this this is the pleb way to do it so you can actually override the plus operator and any other arithmetic Operator by the way to tell the compiler what I want to do when I when I plus these two things together so now all I need to do here is do base stats plus leveled stats and that will just run this logic here and combine it for me so now that's all I need to do through my code and it saves uh obviously you know doing that all the time and if you haven't noticed Unity actually does this all the time for example they've got their quaternion and their Vector classes structs which override all the arithmetic operators and also game objects override the equality operator too which is why you might notice that a quality Works differently in unity sometimes but yeah overload all the things this one is another pretty obvious one but if you've got a bunch of commonly used Logic for example uh when I spawn my character they scale in and there's also a bit of talk on on spawn so I've pulled that out I've got a scale on awake and a scale and spawn and also talk on spawn and now I can just Chuck those on whatever objects I want and all those objects will have that functionality without actually having to do it in each individual script obviously keeps your code dry and if you've ever heard the word composition or people say composition over inheritance this is exactly what that is just don't be a composition Nazi and never use inheritance they both have their uses and they should both be used all right so now for this final tip I feel at this point I may have gained your trust a little bit uh I made an 11 minute video on this and there was just so many people hating on me for trying to make them use naming conventions oh golly my headband uh all I ask is if you are a developer and you are doing public and variable in lowercase and you are doing private and another variable uh also in lowercase and then you're going into your functions uh third variable uh also in lowercase unia have three differently scoped variable types all named using the same naming convention so that means if you're in in the midst of a big function here with 10 different variables and tons of logic you have no easy way to know if you are changing a locally scoped variable or if you're changing a private or a public variable because they're all named the same and then for example if you come and ask me or another senior Dev for help and you paste your code they have to spend extra time passing to see where all your variables are coming from whereas in their code they can just go back have a look at code that they did three years ago and see exactly without actually cross-referencing and scrolling up and down to see where the variable is coming from so to fix this all you need to do is make your Publix Capital make your privates underscore and then your local variables can just be normal under non-underscore camel case there are a lot of naming conventions but as long as you do these three your code readability goes way up even for yourself I promise you once you look back on your old code and you're using naming conventions you're saving a few seconds each time right it adds up it just makes it easier to read I promise you just give it a shot and by the way like I'm not even saying use these exact ones like if you don't like underscores so be it but find three unique naming conventions and make sure that public private and local are different they're unique and uh you will I promise in years to come you will thank me and holy moly last time I had so many uh people they're like oh man I've been coding for 35 years I've never used naming conventions I I've got along just fine cool but you would have got along a little bit better if you did use naming conventions uh it that like it takes very little effort and it makes it easier for you and everybody else to read so uh hopefully you don't hate me after that one I know it's a very controversial topic but that sums up my tips video hope you enjoyed subscribe and see you later
Info
Channel: Tarodev
Views: 85,446
Rating: undefined out of 5
Keywords: unity serialize, unity performance, unity tips, setpositionandrotation, unity beginner, unity3d, playclipatpoint, operator overloading, learn unity, tarodev, unity tutorial, game dev, unity tutorial 2022, c# unity
Id: Ic5ux-tpkCE
Channel Id: undefined
Length: 11min 40sec (700 seconds)
Published: Fri Nov 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.