- Home /
Access function in another script cause Object reference error
I have a ScriptA
in this Script I have a listener on a Button and I want that this listener invoke a function that access another function in the ScriptB
So I have tried this code in ScriptA
:
confirmButton.onClick.AddListener(MyConfirmFunctiom);
function MyConfirmFunctiom(){
var myScriptB:ScriptB=GetComponent(ScriptB);
myScriptB.OpenMainDoor();
}
Unfortunately I get this error
NullReferenceException: Object reference not set to an instance of an object
Answer by Cherno · Jun 23, 2015 at 04:57 PM
Your myScriptB variable is null. Check if the GetComponent function actually returns the component before trying to call a function in it. Chances are that you gameObject doesn't have a ScriptB component.
confirmButton.onClick.AddListener(MyConfirmFunctiom);
function MyConfirmFunctiom(){
var myScriptB:ScriptB=GetComponent(ScriptB);
if(myScriptB != null) {
myScriptB.OpenMainDoor();
}
else {
Debug.Log("" is null!");
}
}
Your answer
Follow this Question
Related Questions
GUI.Box NullReferenceException 0 Answers
Slider to control object rotation cause Object reference problem 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Can't get 4.6 GUI Button to Load Scene (Solved) 1 Answer
Gui Text Script 4 Answers