What's wrong ? [Help]

Hello
i created two java scripts and i wanted to get a variable from the second script which is S2 and modify it in S1
S1 :

#pragma strict
var s2:S2;
function Start () {
s2 = GetComponent.<S2>();
}

function Update () {
s2.rb.useGravity = false;
}
 S2:
 
#pragma strict
var rb:Rigidbody;
function Start () {
  rb = GetComponent.<Rigidbody>();
  rb.useGravity = true;
}

function Update () {

}

Error :
NullReferenceException: Object reference not set to an instance of an object
S1.Update () (at Assets/S1.js:8)

NullReferenceException means that the variable used (in line 8 this case) has not been assigned any value yet.

Is the S2 script on the same GameObject? If yes, then changing

function Start () { s2 = GetComponent.<S2>(); }

to

function Start () { s2 = gameObject.GetComponent(S2); }

would certainly help. If not, then drop the whole code in the Start() of S1 altogether, and assign the value by dragging the game object in the editor.

Try this out, and tell me if it works. :smiley: