I’m working on my second iOS project, and I have a few destructible environment objects I’m working with. Each part of the destructible object has to have a script reference to every other part of the destructible object. I have a few more of these destructible objects to code, but I wanted to stop and make sure that calling gameobject.getcomponent(“name of object”) wasn’t too expensive (it’s currently only being called in the start function). Basically, for this object that breaks into 4 pieces, each of those 4 pieces has a GameObject.Getcomponent to find the script of the other 3 pieces in the object. Should I be worried about this clogging up my iOS game?
Any help is greatly appreciated
Try this thread
http://answers.unity3d.com/questions/15451/which-is-faster-gameobjectfind-or-gameobjectfindwi.html
Sounds like you are doing that in start and cacheing the hook which is the way I usually see it recommended. You did put your example in “…” as if it were a string so I’ll add this from here:
Do not use the version of GetComponent that takes a string. When you call GetComponent without a string the return value is already of the correct type and you can access your custom variables and methods. If you call GetComponent(“TargetScriptName”) you will get your script, but the compiler doesn’t know what type it is so you cannot access your custom variables and methods [unless you manually typecast]
Just a tip worth remembering.
Not really. If you’re that concerned about it, you can assign the resulting component to a variable so it can be directly addressed. That will save you about three flops. I really doubt you would ever notice any difference unless you’re addressing it every update or something.
“Premature optimization is the root of all evil”
Don’t bother cacheing a reference to your components. It will maybe save an unnoticeable time, but it will be a horrible mess if the component gets destroyed, or changed.