- Home /
Question by
linusfessler · Jan 23, 2015 at 09:02 PM ·
functionclassesbetweencalls
Function call from one class to another not working.
Hi, I have a function in class1 which I try to call from another class2:
Code in class1:
function AddCategoryListener (button: Button) {
button.onClick.AddListener( function() {DoSomething(button);} );
}
function DoSomething (button: Button) {
//...
}
Code in class2:
GetComponent (class1).AddCategoryListener (a.GetComponent (Button));
At runtime, when the code from class 2 is being executed, I get a NullReferenceException in the last line of class 2.
But if I add the following infront of the last line it never prints "null":
print (class1);
print (a);
print (a).GetComponent (Button));
Could anybody help me with that?
Comment
Best Answer
Answer by KurtGokhan · Jan 24, 2015 at 12:27 AM
I guess what is null is onClick delegate. You should instantiate the onClick delegate in Start/Constructor of your class1 or before calling AddCategoryListener.