Best way to GetComponent with #pragma strict

So after many hours or trial and error, I finally got my code working with #pragma strict. I'm caching WheelColliders in the Start function, but I'm not sure the way I'm doing it is the best/fastest way. Here it is bellow (developing for iPhone):

// Defines the WheelColliders and collision check bool value...
private var frontWheelCollider : WheelCollider;
private var rearWheelCollider : WheelCollider;

function Start () {

 // Cache reference to WheelColliders so it doesn't get called on every frame.
 frontWheelCollider = GameObject.Find("2dBike/Collider/frontWheelCollider").GetComponent(WheelCollider) as WheelCollider;
 rearWheelCollider = GameObject.Find("2dBike/Colliders/rearWheelCollider").GetComponent(WheelCollider) as WheelCollider;

}

I know that "Find" is slow, but does it matter when it's only called once on start? Could I cache both WheelColliders with a more efficient way?

Thanks in advance!

You can use

GetComponent.<WheelCollider>()

instead, which doesn't need casting at all. But the way you have it right now is good enough, it's just a little more longwinded.

Do what Mike said, but yes, you can certainly cache the references. Having a bunch of Find functions can add up. I like to use the Reset() function for this. You can either call that function with the drop-down menu, on any MonoBehavior, or call it with another Editor script, which I do, for two reasons:

  1. You can Reset() as many scripts as you want, at once.
  2. Reset() cannot be an overridden, so whatever code UT puts in for Reset(), you're stuck with, for the time being. That means that all your value type variables and structs will be set to zero, if you use the drop-down menu. Wheel Colliders are not a problem, because WheelCollider is a class, not a struct, and reference type variables won't be overriden this way.

Here is the basic idea:

#pragma strict

@HideInInspector @SerializeField private var frontWheelCollider : WheelCollider;
@HideInInspector @SerializeField private var rearWheelCollider : WheelCollider;

// Cache references to WheelColliders so nobody has to wait for the game to start!
function Reset () {
    frontWheelCollider = GameObject.Find("2dBike/Collider/frontWheelCollider").GetComponent.<WheelCollider>();
    rearWheelCollider = GameObject.Find("2dBike/Collider/rearWheelCollider").GetComponent.<WheelCollider>();
}

Note that if you're going to use an Editor script to serialize the variables, you don't need to call it Reset(). I just use "Reset" because it's a good name, and if UT ever allows us to properly override their Reset(), as I've asked them to do, via Bug Reporter, it will be handy to have this all doable without having to define a custom drop-down menu item.