- Home /
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.
Answer by Moor · Nov 27, 2013 at 05:44 AM
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
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?
Answer by Huacanacha · Nov 27, 2013 at 05:45 AM
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 Diamond$$anonymous$$anger as a type now... Error is in the first line
Your answer
![](https://koobas.hobune.stream/wayback/20220613122914im_/https://answers.unity.com/themes/thub/images/avi.jpg)