- Home /
[Question] Optimization Editor vs Script?
Hello, From what i've read here in this topic
Getcomponent (specially in childred) are heavy for performance.
Does setting it on the editor makes difference? and is it bad practice to use Getcomponent (writing it on the script) instead of just assigning it from the Editor (if possible to assign).
Tldr: should i prioritize assigning components from editor than assigning it from the script? Thanks!
Answer by JVene · Aug 12, 2018 at 05:19 AM
There aren't many occasions when you can assign a component to a variable in the editor.
While it is true that GetComponent is a heavy call, the real point isn't about avoiding the call itself but avoiding its use within the context of a repeating method like Update or FixedUpdate. Where possible the component should be obtained, saved to a variable within code and referenced from that variable in repeated code. When that can't be done in Start or Awake (there can be synchronization issues with those), the best alternative is to perform lazy initialization of the member variable (simply check it for null and call GetComponent once when it is null).
Your answer
Follow this Question
Related Questions
Finding Children question 3 Answers
change pivot of parent game object 1 Answer
Removing index of an integer list from a child Game Object 2 Answers
Get Child from a parent 3 Answers
Instantiate object as child of exsisitng game object 1 Answer