most efficient way to declare and assign variables

cache - ing ?

Hello, first of all I hope my terminology is correct.

What is the actual best practice for, (and for what types), declaring loop variables in setup versus declaring and assigning variables every frame ?

cache loop variable =>

private var i : int; // case 2 : requires declaring

function Update () {
    for (var i : int = 0; i < 10; i ++) {}; // case 1 : should I use this
    for (i = 0; i < 10; i ++) {}; // case 2 : or this
}

cache this.transform as myTransform =>

private var myTransform = Transform; // case 2 : requires declaring

function Start() { myTransform = this.transform; } // case 2 : requires assigning

function Update () {
    this.transform.position = Vector3.zero; // case 1 : should I use this
    myTransform.position = Vector3.zero; // case 2 : or this
}

or could I adopt a general rule and say “any component I modify more than twice I shall cache (declare; assign at Start) and use that”?

I am developing for iOS, and I have very little understanding with factors that have to be considered involving : memory, processor demand, garbage collection and dumping, removing instantiate required objects to be removed from memory for long play sessions. Of course I can research all the above as I am at least aware of them, But for the simple case of cacheing a Transform, a GameObject, and a Loop Variable; what is the best recommended practice? Thankyou.


Edit : Another case-scenario, now considering a large array. 2 situations, array items are only needed for calculations once, and array of same length each frame used for calculations :

When using the values in (large, static) arrays once (on startup).

private var myMesh : Mesh;        // case 2
private var verts : Vector3[];    // case 2
private var uv : Vector2[];       // case 2
private var tris : int[];         // case 2
private var normals : Vector3[];  // case 2

function CreateMesh() // e.g. Start
{ 
	var myMesh : Mesh = this.transform.GetComponent(MeshFilter).mesh as Mesh; // case 1
	var verts : Vector3[] = new Vector3[600];   // case 1
	var uv : Vector2[] = new Vector2[600];      // case 1
	var tris : int[] = new int[1200];           // case 1
	var normals : Vector3[] = new Vector3[600]; // case 1
	
	myMesh = this.transform.GetComponent(MeshFilter).mesh as Mesh; // case 2
	verts = new Vector3[myMesh.vertices.length]; // case 2
	// then other arrays
}

When changing the values in (large, static) arrays every frame

private var myMesh : Mesh; // case 1 & 2

private var verts : Vector3[];    // case 2

function Start() 
{
	myMesh = this.transform.GetComponent(MeshFilter).mesh as Mesh;
	CreateMesh();
}

function AnimateVerts() 
{
	var verts : Vector3[] = new Vector3[myMesh.vertices.length];      // case 1
	// verts[] already exists, no need to set size (no verts added or removed)  // case 2
	
	// each item in verts[] get revalued ....
}

Regarding this:

private var i : int; // case 2 : requires declaring

function Update () {
    for (var i : int = 0; i < 10; i ++) {}; // case 1 : should I use this
    for (i = 0; i < 10; i ++) {}; // case 2 : or this
}

Case 2 is generally a bad idea, unless there’s a clear benefit, such as caching components. For standard variables, always make them local where possible, not global…not only does this make the code a lot cleaner, but it’s (very slightly) faster. Even caching components can be a waste of time, unless you’re accessing them a lot. I would say the only time you would cache variables as a general rule is when the Unity docs specifically tell you that a particular function is slow, such as with GameObject.Find.