10 Unity Tips You (Probably) Didn't Know About

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here are 10 awesome Unity tips that will help save you time and headaches ready let's go let's say we have a script that makes our player flash whenever we take damage so to get a reference to it we might say get component and get a reference to our damage flasher and if we were damaged then we'll call The Flash function now obviously this will work fine so long as we remember to add the damage flasher script to our player but if we don't we're going to get a lot of null reference exception errors but maybe you're using this script for a lot of game objects in your game and so you want this to be optional what we can do instead is use try get component and use the out keyword to assign the component to our variable if it's found and this will actually return a bull value so we could do something like if it finds it then yes we can Flash else we can't and then add this little check down here now we don't get any errors sometimes when you're coding it can be really useful to be able to comment out a whole bunch of code all at once so to make multiple line comments all at once highlight the code you want to comment out hold contrl k then crl C and to undo that press crl k then crl U and similarly if you want to use a block comment for a bunch of code then highlight the code and press contrl shift back slash and it'll be the same keys to undo that and let's do one more if you want to actually go and see the code for a function just click it and press F12 it'll take you to the correct script at the correct function which can really speed up debugging sometimes for any number of legit reasons you may find that you need to change the hierarchy order in some of your characters like adding new game objects or reparenting certain things but if these characters are already animated then this is going to break everything since the paths do not automatically update now the good news is it's pretty easy to fix and it only takes a couple of seconds highlight the broken path and then press f2 which will allow you to rename it to the New Path then type in the New Path and now your animations will work just fine again although Unity if you are listening please make a built-in fix for this that would be really great to get rid of a reference in the inspector you don't need to click on the nipple and select none you can literally just click it and hit the delete key done so you just finished a function and you want to test it out quickly to see if it works instead of coding in key presses or anything like that there's a built-in attribute called context menu so we'll put that above our function and give it a name now let's play our game and you can rightclick the script and now we can click on the new line that appears there to run our function this annoying boiler play code that shows up on every single new script you create let's fix that and make it blank or make it whatever you want cuz we're going to modify the unity template in Windows you're going to go to your C drive program files Unity Hub editor whatever version you want to change and yes this change does unfortunately only work on a per version basis editor data resources script templates and the one we want to change is 81 new Behavior script and I'll throw the link up for Mac users as well but before you try to change it it's very likely that you won't have the proper access to do so so right click it and go to properties and go to security and under users I'm going to edit my permissions and add the right access now if we open up 81 we can delete or add whatever we want save the file and now when we create a new script it'll reflect the changes you made to that template file if you add the system. Diagnostics namespace to your script this will give you access to the stopwatch class which is a high Precision timer and because of how highly precise this timer is it is the best tool that you can use for performance benchmarks so let's set up a test performance function and start the timer and then we're going to do the actual thing that we want to time and then stop the stopwatch and then we'll get a reference to the elapsed milliseconds and print that out at the end so let's say we want to know how long it takes to perform a simple find game object with tag call and we'll just run this in the start function and when we run well this takes 0 milliseconds because it's too fast to be picked up but we can add what's called elapsed ticks and one tick is equal to 100 nond or 0.000000001 seconds put another way 1 millisecond is equal to 10,000 ticks so you can see that it did pick that up it's just below a millisecond right now of course if we did something a little more Hefty like run find game object with tag 50,000 times now it'll show a value on the millisecond timer not too long ago if I wanted to rename a script what I would do is rename the script file then open the script re name the class then doubleclick the errors and fix those don't do that instead open the script doubleclick the class name and press controlr and controlr again rename it and press enter it's going to rename the class all the references and the script file name as well and also just in case you weren't aware this is the same shortcut that you can use to rename all variables or function names as well the the reset function isn't something I see get used too too often but it's actually really handy and can save you some hassle especially when you combine it with the require component attribute and I'll give you an example let's say we want every single enemy in our game to share the same base functionality like an aggro radius for example now for something like Health it makes sense to set it up this way so that we can set each enemies Health manually and easily create prefabs of different enemies with different Health values but let's say we want our aggro radius to be the same for every single enemy in the game so we can set up a variable for a circle collider and let's make it required since every enemy will require it so if we do it this way when we create a new script that inherits from enemy base when we drag it onto an enemy it attaches a circle collider but it's not a trigger and it has the default radius of 0.5 so let's change that by adding a reset function a reset fun function gets called when you click this button here but it also gets called whenever you add a new component to a game object so knowing that let's get a reference to our Circle collider and change it to trigger and change its radius now when I add this script to any enemy reset is going to get called and it'll automatically add a circle collider that is a trigger at our specified radius another easy way that you can test certain things with is with the execute in edit mode attribute and this can be really useful because you can even run the update function in the editor without being in play mode so let's say you had a spawner and you want to spawn something in every single second and you just want to test it out really quickly so you can write it like normal and then just add the execute and edit mode attribute at the top and you can see we have a little circle spawning in every second even though we haven't hit play yet and if you want to be able to stop this quickly the fastest way is to just disable the script and there you go I really hope you enjoyed and found these tips useful let me know any additional tips in the comments down below a lot of what I learn is from you guys and your comments actually so if you have any that should be added to this list leave it down below thanks so much for watching bye I want to give a very special thank you to all of our Hall of Fame patrons yab yond Christopher Nichols Zandra kastler Fontaine weight brain waves to binary couch KB at bird Tech games and Ian oral as well as our Early Access patrons Ken waight Mason Crow Mr D Yan liquid egg Alexander press Jude gaves Felipe G do Santos obber Franchesco lamata Bill guo alone on Mars Alex fredman Danny rlif arnan s shaig Neil Ben kerger John wisman lucky Tales Aiden serve Adar Kumar merler and Anastasia sham Molina if you choose to support us on patreon you can get early access to all of our YouTube videos monthly Alpha builds and more
Info
Channel: Sasquatch B Studios
Views: 9,493
Rating: undefined out of 5
Keywords: unity, unity2d, unity tutorial, sasquatch b, unity beginner tutorial, game development, unity tips, unity tricks, unity tips and tricks, unity tips and tricks 2023, unity tips for beginners, unity pro tips, unity hacks, unity best practices, game dev tips, unity save time, unity easy tricks, unity best tips, pro tips, unity beginner advice, unity things to know, unity things you didn't know you could do, unity hidden features, unity unknown features
Id: GKk5rp2DxII
Channel Id: undefined
Length: 8min 58sec (538 seconds)
Published: Thu Oct 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.