Problem calling another scripts function (JS)

So I have a script A trying to call script B’s function. I get a NullReference because I am not able to get the script correctly, but I’ve followed the exact stuff I’ve seen on the forums. Why am I having such a problem with this?

Script A:

var diamManager : DiamondManger;
var diam : int;
diamManager = gameObject.GetComponent("DiamondManger");

function OnGUI() {
	
    diam = diamManager.Diamonds();
}

Script B:

 public function Diamonds(){
	Debug.Log("diamonds = " + diamonds);
	return diamonds;
 }

the diam line in the OnGui function is where I am getting my NullReference error.

2 Answers

2

var diamManager : DiamondManger;

just assign the script in the inspector and don’t want this

diamManager = gameObject.GetComponent("DiamondManger");

The problem to this is I the inspector will not let me assign the script. It just wont let me drag into it with the correct script

that means, check the spelling of script name

Checked it, doubled checked it, even changed it to a lowercase first letter. Nothing. I know right? In monodevelop it doesnt even turn blue... hint?

You need to put the “diamManager =” line inside the Start() function. The object/script hasn’t been fully initialized when you call GetComponent so the component you are looking for doesn’t exist yet.

The problem with this is, it is not recognizing DiamondManger as a type now... Error is in the first line