Reference Not Set toInstance of Object

So I have a JS making a call to a CS script using GetComponent.

public static var upbutton : UpButton;

function Update () 
	upbutton = GetComponent(UpButton);
	upbutton.WasUpPressed(); //error here

Then there’s the CS

public class UpButton : MonoBehaviour 
	public static bool upPressed = false;
	void OnMouseDown()
		upPressed = true;
	public bool WasUpPressed()
		return upPressed;

Problem is in the JS error line “upbutton.WasUpPressed();” the error says
“Object reference not set to an instance of an object”

In this case, it means:


returned null, thus upbutton is null and thus you can not call the WasUpPressed function.

Do you actually have the component “UpButton” attached to the same game object you have the JS on?

Also, when you do this:

public static var upbutton = false;

You probably make upbutton a boolean variable.
You must have meant doing this:

public static var upbutton;

Since you do not know what the variable is going to be, as you want to use duck typing.

But you should prefer doing this:

public static var upbutton : UpButton;

letting the compiler know before hand what you meant your variable to be and avoiding surprises during run time.