What's wrong ?

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)

bump

s2 isn’t on the same gameobject as s1 most likely… or, if they are the contents of Start in s2 should be in Awake

1 Like

After calling GetComponent check to see if its null, also GetComponent will only find objects that are on the same GameObject so if you have it somewhere else it will return null.