2 Questions about Update and OnGUI functions


i am making some games on Unity2d and C# and i have 2 questions about Update and OnGUI

1-What is the difference between putting my GUI codes on Update() or OnGUI
for example:

void OnGUI()

void Update()

2-i am using lots of codes on Update function, will that decrease the fps or something for the player? (the game will be on ios/android devices)

Thank you very much

OnGUI gets called multiple times per frame. It gets called at least twice, once with a layout event and once with a repaint event. If you click a GUI element it gets called with mousedown and mouseup (not always in the same frame). It can go higher in some circumstances.

In contrast Update will only get called once per frame.

Based on this you would typically prefer Update over OnGUI, unless you are using the GUI or GUILayout functions. For something that does not change every frame you would typically not do it in either function, but only call it when the value changes.

To use the unity GUI and GUILayout drawing functions you must put them in OnGUI. That is its purpose and it ensures that they get drawn on top of everything else. Many of them simply will not function in the context of Update.

In terms of doing other work, both OnGUI and Update get called once per frame so you should be limiting the work you do in either to only what is necessary.