how to optimize your code?

Which are the most common mistakes made by coders for non optimized code that is letting down the FPS.
What things you should be aware off, what to avoid …
What are the best ways of coding ? Best methods…
C# or Java …

Just wondering those questions.

Big question, so many answers.

1.don’t duplicate any code. like… ever. if you’re duplicating code, you probably don’t know about some way there is to design something to accommodate for multiple cases.
2. have as few things running every frame as possible. coroutines are very useful things.

A few very common ones in Unity…

Using “finding” or “getting” functions each frame in Update rather than storing a reference to the found object. Things like Transform.Find and GetComponent can be pretty time-consuming so it is much better to execute them once in the Start function rather than have something like:-

otherObject.GetComponent(MyScript).MyFunction();

…in Update or FixedUpdate.

Using properties that look like simple variable references but actually execute a lot of code behind the scenes. There are things like Mesh.vertices that need to copy a whole array each time they are used, so harmless-looking things like:-

for (i = 0; i < mesh.vertices.Length; i++) {
  mesh.vertices[i] = mesh.vertices[i] * 2.0;
}

…actually copy the whole vertices array twice for each iteration of the loop (imagine this code in the Update function :wink: Another similar one is Vector3.magnitude (and the related Vector3.normalize). This seems to be a simple property of the vector but it calculates a square root each time it is used. Again, calculate the value once and then store and reuse it.

Instantiating objects and later destroying them when they are no longer needed is sensible from a logical point of view but it is much slower than keeping the objects and just “hiding” them when they are not needed. This can involve keeping the objects off-camera or disabling the renderer.

Not code related but i always found overly large textures, multiple models on screen and high mesh counts can reduce frame rate. Consider, normal maps, LPM and also using textures no larger than 512. Real time shadows will also hamper performance and reliance on Networking (slow server response rate may have an impact)

If you have Unity Pro, the profiler is your absolute best friend when it comes to trimming milliseconds off your code execution time. Read the manual page about the profiler and how to deep-profile just small sections of your code. This way you’ll spend all of your time optimising only what’s slowing you down rather than just making heaps of random changes in the hope that one of them will eventually make it go faster.

Some coordinate transforms are massively expensive. Try to avoid doing lots of WorldToViewportPoint or vice versa like transforms every frame. My current baby-with-the-one-eyebrow is how to deal with up to 10,000 WorldToScreenPoint calls per fixed time step for all my custom UI lines in my strategy game. I’ve lost a few battles but thanks to the profiler I at least know I’m fighting in the right war.

Use squared distance comparisons when possible to avoid the square root overhead of the regular distance calculations. If you’re checking distances every frame then consider adding in some other system that disables/enables the need to even do those checks. Unity’s trigger system is ridiculously fast, abuse it as much as you can to make sure you’re only updating stuff that’s actually relevant to the player.

Store local references to components, even components of the current MonoBehaviour’s game object. this.transform has almost the same overhead as any other randomGameObject.GetComponent() call. Using something like localTransform = this.transform; in Awake() can speed things up a lot.

If you use Javascript instead of C# make sure you always use static typing, use the #pragma strict directive at the top of each script to force static typing.