GameObject is giving NullReferenceException on UnityEvent.Invoke

Hi,

I’ve set a public GameObject of type Text. Dragged and dropped the Text UI into this GameObject.
I have a button that then invokes a UnityAction, but when it enters that function (TestModalPanel.cs: TestOkFunction()), it can’t find the Text, giving a NullReferenceException.

Does public GameObjects not get passed when you invoke an event? But this doesn’t seem to be the case when I stored a bunch of Textures (see TestModalPanel.cs, TestYesFunction())

NullReferenceException: Object reference not set to an instance of an object
TestModalPanel.TestOkFunction () (at Assets/Scripts/TestModalPanel.cs:91)
UnityEngine.Events.InvokableCall.Invoke (System.Object[ ] args)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[ ] parameters)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[ ] parameters)
UnityEngine.Events.UnityEvent.Invoke ()
UnityEngine.UI.Button.Press ()

public Text coins;
void TestOkFunction() {
Debug.Assert (coins != null, “Text Coins can’t be null. Assign a GameObject.”);
//if (coins != null)
{
numCoins += 100;
coins.text = “100 coins”;
}
}

I have my project uploaded to GitHub: GitHub - elissatong/UnityGenericModal: Unity GUI based generic modal window
Greatly appreciate any help you can give.

Thanks

2248415–150187–TestModalPanel.cs (2.46 KB)
2248415–150188–ModalPanel.cs (2.32 KB)

I realized what my problem was. It turns out I had TestModalPanel script attached to my Manager GameObject, and the TestPanel GameObject. I only updated the public GameObject in Manager but not TestPanel. The test buttons (Button01 to Button03) was using an OnClick event based on the TestPanel GameObject, so elements were still coming in as null references! The Unity GUI tutorials here actually have the TestModalPanel script attached to each test button (Button01 to Button03), but I found that repetitive, hence I tried attaching a script only once to a global GameObject.