- Home /
if Fixed Update calls outside functions
Imagine all variables and custom functions are declared and defined in Awake, but called from within Update, or FixedUpdate for the physics functions.
If my understanding is correct, functions don't do work until they are called, and Unity calls Update and FixedUpdate (and their content) by frame and by time respectively, and it is the priority with which Unity handles them (and their content) that makes FixedUpdate preferable for physics.
So if Update content calls only functions not related to physics, and FixedUpdate content calls only physics functions, why does it matter where the functions are described?
Answer by tanoshimi · Jul 26, 2016 at 08:03 AM
Your question is somewhat confusing.... it doesn't matter where a function is declared in your script (although you can't declare a function inside another function so your description of declaring a function inside Awake() is incorrect). So you can place the function definition:
private void MyMethod() { ... }
At the top of your script, underneath Start(), right at the end - it makes no difference. Whatever you do inside MyMethod will only happen when the method is called, which could be:
void Start() {
MyMethod();
}
or:
void OnMouseClick() {
MyMethod();
}
or:
void FixedUpdate() {
MyMethod();
}
You say functions can't be defined inside other functions, so you recommend Start ins$$anonymous$$d of Awake. But I thought Start and Awake were both functions themselves, so I'm confused about that part of your answer.
But you seem to have understood the point of the question and answered it thus: there is no performance difference between calling a function defined in FixedUpdate, and FixedUpdate calling a function defined elsewhere.
Edit: an explicit confirmation that my understanding is correct would still be appreciated.
Start() and Awake() are both functions.
I don't recommend Start ins$$anonymous$$d of Awake - they get called at different times and are used for different things. As a rule of thumb, you use Awake to initialise anything related to this gameobject only. You use Start to initialise anything that depends on other gameobjects.
There is no performance difference between writing code directly in the body of FixedUpdate, or writing code in a function that gets called from FixedUpdate. I think that's what you're asking.
You said you can't define a function inside another function, so you recommend using Start rather than Awake. But I thought Start and Awake were both functions themselves, so I'm confused by that part of your answer.
But you seemed to get the point of the question and answered it thus: there is no performance difference between calling a function that is defined in FixedUpdate, and FixedUpdate calling a function that is defined elsewhere.
An explicit confirmation that my understanding is correct would still be appreciated.
Your answer