Getting a NullReferenceException when trying to assign a script variable

I’m trying to assign a script as a variable so I can easily access variables within this script. I’ve done this before, but for some reason in this specific instance it’s constantly throwing errors when I try to use the assignment, as if it’s unable to find the script I’m referencing.

I’ll post my initialisation code here, even though I was still getting this trouble when I just dragged and dropped the component into the relevant variable slot in the Unity window.

public var player: GameObject;

public var x: int;
public var y: int;
private var found: boolean;

var board: GameObject;
var mainCode: StuffHolder;
mainCode = FindObjectOfType(StuffHolder);
private var size: int = mainCode.size;

So I am 99% sure I didn’t screw this up in code and I’m asking… what other ways could I have screwed this up?

mainCode = FindObjectOfType(StuffHolder);
private var size: int = mainCode.size;
i think you should put these two into one of those initialization functions, like Start() and the like.

otherwise, assuming that your StuffHolder class have a variable called size, it might be that the variable is not initialized and you’re trying to access it, causing it to throw null reference exceptions.