- Home /
Is performance related to the script size that is called by GetComponent?
Hello everyone,
There are times that we need a tag and an additional tag like property. For example I am working on an "insect catching" game, where I check the tag if insect .. but for each insect type there are special characteristic. like if you catch blue fly you earn 5 points, red butterfly 12 coins .. etc
I just have several solutions and searching for the optimal.
I can create a new script and have a single public var (mainly string) and call GetComponent each time.
Or add a public var to an already existing script , but I doubt calling GetComponent for a large script (of few hunders lines of code) for just getting a string.
My question is: Performance wise is it the same calling GetComponent for an empty script (with a single public var), and calling GetComponent for a huge (1000 line) script.
Or else if there's a another solution offered by you.
Thank you
Answer by JohnnySunshine · Jul 05, 2013 at 01:01 PM
Yes, it takes the same amount of time for "big" and "small" scripts.
I also think that calling GetComponent each time you click on an object shouldn't have a large impact on performance, but if you're concerned, use the profiler. It does a much better job of identifying potential bottlenecks then us puny humans do :)
While calling GetComponent each time you need access to it isn't as slow as creating or destroying objects, it's still slower than caching a reference and using that.
private SomeScript someScript;
void Awake()
{
someScript = GetComponent<SomeScript>();
}
void Update()
{
someScript.Some$$anonymous$$ethod();
}
Your answer
Follow this Question
Related Questions
What am I doing wrong when disabling a script? 1 Answer
Looping through materials on a mesh with foreach() in Start() is causing lasting FPS drop? 1 Answer
Get Component from Instantiated Prefab 1 Answer
Empty public script variables. 1 Answer
how to make one script change a variable in another scipt 2 Answers