- Home /
public variable vs GetComponent()
You created a script that needs another one's variables, then:
What you guys think is best to use?
public variable and drag them from the inspector or GetComponent<>() on Start() ?
Is there any performance difference?
Thanks
Any performance difference will be negligible. There are a number of posts that indicate dragging and dropping in the Inspector in most situations can lead to design and maintenance headaches for more complex projects. The exception is within a prefab or possibly within other parent/child relationships where the linkage relationship is easy to understand and maintain.
Answer by YoungDeveloper · Jan 13, 2014 at 06:53 PM
The difference would be so small, it wouldn't really make any difference. If you need to launch getcomponent only time to time or once, then saving script wouldn't be really the best way, because memory will be reserved for that variable, even if you wouldn't use it most of the time. Presaving script would make a difference if you need to call something many time, or almost every frame.
For example if you need to getcomponent once, and call some method, like die method, then all what you do is:
GetComponent<SomeScript>().Die();
There's really no need to presave component all that time you are alive.