Object Reference Not Set to an Instance of an Object?

I can’t seem to figure out why I’m getting this error, but I’m sure it must be a simple explanation, can someone please help? I have these two scripts which I have attached to 2 different cubes :

Script 1:

private var variableInstance;
var variable : int = 0;

function Start () {

variableInstance = GetComponent(script2);


function OnMouseDown () {

variableInstance.variable2 = variable;


Script 2:

var variable2 : int = 5;

I have the first script attached to one cube and the second attached to the other cube. I believe this should allow me to click on cube one and the value for “variable” should be set to the value of “variable2”. But obviously its not working, I get no compiling errors until I enter play mode and click on the first cube. what am I doing wrong?

Also here’s the exact error:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
script1.OnMouseDown () (at Assets/script1.js:12)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

Variable and variable2 don’t have any values … Try setting those

You issue is that you have the script attached to two different game objects. How you use it above, GetComponent() will return a script attached to the same object as the script. You must get access to the other game object in order to get access to the script. There are a number of ways getting this access. Here is one:

variableInstance = GameObject.Find("Cube2").GetComponent(script2);

This code assumes that the other game object is named ‘Cube2’. Change the string to whatever you have it named.