- Home /
Best Performance: Tag or GetComponent + Rendering/Diactivating
Greetings everyone, i searched related topics but didnt get the answer i needed , well my problem is simple. I have 1600 exacly objects with a mesh renderer,a collider,a mesh filter and a script of my own. What i need it to make those objects not consume any performance and i created a script that does it for me now i want to know if Disactivating the whole object is equal to disabling its mesh renderer only or disabling both its mesh renderer and mesh filter. And also i want to know if its better for each one of them to have like 6 times a if(GetComponent(...).somevariable == "blahblah") or a if (hit.trasform.tag == "blahblah") [witch makes 9600 of these in the function update].
Using: Unity 3.5.6 pro
If you on a Pro licence you can use the profiler to compare the performance of each option.
It's made to address questions like this.
I'm pretty sure GetComponent() is somewhat slower than checking for a tag, although using tags to deter$$anonymous$$e something might make you end up with a bloated list of tags. I'd advise you to cache the component once and then access it via a variable ins$$anonymous$$d of calling GetComponent six times.
var myComponent:Component;
myComponent=GetComponent(...);
if(myComponent.somevariable=="value")
//...
Deactivating the entire object also prevents any of the scripts on it from running. On runtime i'm pretty sure it would be functionally the same were you to disable the object or turn off everything, although i'm not sure if there's additional optimisations in place that would favor the former in terms of performance.
Answer by NightmarexGR · Dec 30, 2013 at 04:38 PM
Thnx both of you your answers were really helpful and i really had forgotten about the profiler i think what Snake said is right , i will try with tags and if it doesnt work i will use the GetComponent function while i will keep an eye on the profiler , Thnx again for the quick help.
I have to say that the tag way is way more performance friendly because while tested both ways in the tag method my fps was always at $$anonymous$$ax and the getComponent method reduced it to 30 :C
Are you calling GetComponent six times per object per frame? Are you also using it with a String as an argument? (Both are possible sources of the slowdown)
I made a var for the GetComponent but i didnt use a String as an argument.Since i made it work smoothly i dont need anymore help and besides that i found a pro asset : Occlusion Culling that does exacly what i was trying to create on my own. Thanx for your answers.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125142im_/https://answers.unity.com/themes/thub/images/avi.jpg)