Quick question about global variables. Two ways. Are they different?

Just a quick question:

Are A and B scripts exactly the same? I'll be able to work with them as a global variable, without affecting other gameObjects with the same script and etc? Are they just two ways of programming exactly the same thing?

A)

private var lifeScript;

function Start ()
{
    lifeScript = gameObject.FindWithTag("Player").GetComponent(life);
}

function Update ()
{
    var playerLife : life = lifeScript;
    playerLife.life -= 10;
}

and B)

private var lifeScript;

function Start ()
{
    lifeScript = gameObject.FindWithTag("Player").GetComponent(life);
}

function Update ()
{
    lifeScript.life -= 10;
}

Thanks.

They are not "the same". You are creating another variable in the first example. But both of these variables hold reference types, not value types. That means that when you change "lifeScript.life", or "playerLife.life", yes, you will be doing the same thing. So, might as well use the second example, considering playerLife isn't adding anything new.

Here is more information on value vs. reference types.

Also, you should name your classes with an uppercase letter first. i.e. "Life", not "life". And use GameObject.FindWithTag, not gameObject.FindWithTag; using a specific game object, with the latter method, is unnecessary and misleading.