Hi, I'm using GetComponent to access variables in another script. I read somewhere that it was "relatively" slow. Is there a better way? Just one more thing, FPSWalker seems to lag my program too. Not when I'm moving forward, just when I want to rotate left or right. Thanks, Chris
If you're using GetComponent a lot (like every frame), cache the component. Otherwise don't worry about it. Describe "lag", because there's nothing in the FPSWalker script that's slow. --Eric
The trick here is to avoid calling it repeatedly, call it once and store the reference obtained in a variable then use that variable later when needed. Here are two simplistic examples: Code (csharp): // Bad function Update () { var yourScript : ExampleScript = GetComponent (ExampleScript); yourScript.foo = 5; } Code (csharp): // Good var yourScript : ExampleScript; function Start () { yourScript = GetComponent (ExampleScript); } function Update () { yourScript.foo = 5; } Again, the above are extremely simplistic but designed to prove a point. If you know you're going to need to access a component repeatedly then get a reference to it one time, then use that stored reference when necessary. This avoids the need to call GetComponent() repeatedly which can in fact be a performance drain if done in excess. Please note that this is a general strategy you should keep in mind across the board, any "finder" functions like that have to do some amount of work to look-up/find the item in question and that takes time (even if only tiny slices). But lots of tiny slices can add up, so cache references!