Unity Tutorial – TIPS and TRICKS for Optimising Scripts

ACCESS the FULL COURSE here: https://academy.zenva.com/product/unity-game-development-mini-degree/?zva_src=youtube-gamedevmd


Hey, everyone. In this lesson we are gonna go over a couple different scripting opimizations that you can implement in your projects in order to increase performance. As with mobile games, especially, performance is something you always need to keep in mind. So, the first thing we’re gonna look at is caching objects. Now, whenever you wanna access and object inside of Unity, you might think that, oh, the GameObject.Find function, I can just easily find objects that way or get a component. Here in this example, I’m finding the object, Ball, and accessing the MeshRenderer component to change the color to blue every single frame. Now, this is a very expensive line of code and when I say expensive, I mean that it is using a lot of processing power and it takes quite a long time to run in terms of a function. And what we can do with this is actually cache of the MeshRenderer component of the ball instead of having to find the ball every frame and find the MeshRenderer component every frame. So, instead, what we can do is cache it. We can create a private variable for that Ball MeshRenderer and in the Awake function, that is where we will call the GameObject.Find function to find the ball object and get the MeshRenderer component. What this will do is greatly reduce the amount of time it takes to run the Update function as we are only calling the GameObject.Find and GetCompnoent for the ball once in the whole game, whereas before, we were calling it probably around 60 times a second if you’re at 60fps on a mobile. This is just a lot easier and this is really what you should be doing if you do have these sort of large and expensive functions being called quite regularly. Here is also a list of all the other sort of functions that you should really cache if you’re calling them every frame or if you’re calling them quite often. Your GameObject.Find and then you’ll see we got GameObject.FindObjectOfType. Now, this is probably one of the most expensive ones as what this is doing is, like GameObject.Find, GameObject.Find is looking through the scene for a specific object with a specific name. Well, what GameObject.FindObjectOfType is doing is doing the exact same thing as Find but for each of those objects, it is actually pretty much calling the GetComponent function so it’s looking through every object and every component for a specific one and that is actually quite a performance hitting function that you should only really be calling once for an object when it’s been initialized. Then, there is FindObjectWithTag which looks through all the objects for a specific tag. This is actually what the camera does, the Camera.main. Whenever you call Camera.main, this is, behind the scenes, what it is calling. So, when you think of that, it’s really something that you shouldn’t really be calling every frame as well. Same with GetComponent, and of course, Camera.main. Something else you should also keep in mind when creating functions inside of Unity is reducing the amount of time you need to call them. Sometimes you’ll have a really expensive function like something to do with path finding or calculating something quite large.

Free Tutorials:
– Unity: https://gamedevacademy.org
– Phaser: https://phasertutorials.com
– Machine Learning: https://pythonmachinelearning.pro
– Web Dev: https://html5hive.org
– Android: https://androidkennel.org
– Swift: https://swiftludus.org
– VR: https://vrgamedevelopment.pro

Post Author: hatefull