Which Unity functions are generally slow/bad for performance?

As a fairly novice Unity developer I have often found it difficult to find out which Unity functions are particularly heavy operations and should be avoided. Even the Unity documentation doesn’t mention whether a particular function is performance heavy or not. Not knowing beforehand obviously makes the learning process a lot slower because you learn how to code something one way, and then later find that in terms of performance it is an absolute no, and have to start all over again.

I think it would be useful to make a list or groups of lists of Unity functions starting with generally those most performance heavy. Obviously some functions depend on how they are used, so this won’t be a “no exceptions” list, just a general guide for novices like me.

Please feel free to add/remove/make suggestions as I am only guessing here:

Super Heavy

Heavy

Medium

Low (but worth considering)

GetComponent or FindComponent is heavy but there is no harm if you complete these steps at the beginning of the level.

Just don’t put them in your Update method.

What I typically do is populate the components in a local array at the beginning of the level by making all the needed getcomponent calls ahead of time.

This allows me to access these components from my local array during the gameplay, which is of course much faster. But, it takes work.

Not as heavy, but worth knowing that magnitude and normalized of vectors can be somewhat slow since they involve square root computation.

As mentioned in the first link:

If you only need to compare magnitudes
of some vectors, you can compare
squared magnitudes of them using
sqrMagnitude (computing squared
magnitudes is faster).

in order of worst to least. Overlapshpere, raycastall, raycast, and vector3 distance. are all mistakes i made to forse the rewrite of my last project not on your list. they are wonderfull tools but must be limited depending on your project.

something as simple as :

var frame:int;

frame=frame-1;
if (frame ==0){frame=10;

//---now check a heavy sherecast your game needs
} 

can lighten the load on your cpu greatly
it all depends on what you are doing. one thing i found is that the friendly game engine developers with unity created colliers that DO NOT slow game progress. i have tested thousands in a level.

any type of “Find” or “lookup” should never be called more than once! anything more should be stored in a variable or array for fast lookup.

“destroy” should be avoided. things like bullets are recycled…not destroyed.this also reduces the need for instaniate. even though unity does it automatically, there is a term called garbage collection. where a system goes through a laggy proccess of freeing ram memory for things “destroyed”. its better to move bullets under terrain untill recycling or make them invisible when not in use.

debug and print definally slow a game…when code blocks are desireable delete for final builds.

i havent seen evedence of asking"position" or rotation slowing things but if you where worried you could also pack a common request in the top of your update.
i do that just for easier typing somtimes in a longer code.

var tp:Vector3;
function Update(){
tp=transform.position;

if (tp==this){do this;}
if (tp==that){do that;}
}

everyone complains about the gui system being slow.
if you want to pick up on a mouse click on a position. why not put it in your update???

function Update(){
sw=Screen.width;
sh=Screen.height;

mx=Input.mousePosition.x;
my=sh-Input.mousePosition.y;

if(Input.GetMouseButtonDown(0)){
if(mx>sw*.2){if(mx<sw*.3){
if(my>sw*.2){if(my<sw*.3){

//i know you clicked a spot on the screen without a giu button
}}}}}

remember that all unitys funtions are basically something in your script that you dont see. depending on the function, unity could run hundreds of lines of code, potentially on hundreds of different objects in a scene on the frame you call some of these options. then your game waits for a return from unitys scripts/functions.

for large games accomplishing big things professional developers use several technics that are specialized for the problem in your game. such as as only checking a distance for things within a specific camera distance or packing references into super fast arrays or lists for lookup. if you could give us details about your game and what you think might be slowing it, we can give you some ideas on how to get around it!!!