- Home /
splitting a script up into multiple scripts...
So GetComponent, is it that intensive.
I like to have Different functions and stuff all there own script so that whatever i'm looking for I click the script and im right there, I'm worried though that basically having 30 scripts on an object when really there all working in concert will mess with performance as I GetComponent across scripts.
My camera for example has quite alot of scripts on it. Some of them are basically one script thats used only for another script.
I might have a Select & Move script but I have 2 different move scripts and it calls a different one depending on the movement for example. The scripts basically exist only to be called inside select and move, and technically I could just insert the code itself into select and move but I like abstracting it and being able to just call a function and when I want to mess with that function I go to that script.
Is this a bad idea? I learned C++ and it didnt matter because all the code compiled at run time into basically one super script, but having to reference other scripts with GetComponent calls and thereby access them at runtime seems like I might be slowing down the program.
Answer by Seth-Bergman · Nov 16, 2012 at 06:22 AM
well, calling GetComponent over a large variety of components throughout the scene COULD be performance intensive... Particularly if you are doing it EVERY FRAME.. i.e.:
void Update(){
SomeScript someComponent = GetComponent<SomeScript>();
someComponent.DoSomething();
}
SO, if you are accessing these objects FREQUENTLY, you should just run GetComponent ONCE, like say in the Start():
public SomeScript someComponent;
void Start(){
someComponent = GetComponent<SomeScript>();
}
void Update(){
someComponent.DoSomething();
}
It is perfectly acceptable (IMHO) to use references to other components in this way.. (In fact it probably makes your code more legible) But GetComponent should basically only be used whenever you need to INITIALIZE your references (usually at the start, or when the object is created). Just keep that in mind, and you should be fine!
Your answer
![](https://koobas.hobune.stream/wayback/20220613083940im_/https://answers.unity.com/themes/thub/images/avi.jpg)