Cant access other gameObj's function from script (C#)

I’m attempting to dynamically assign a method from a “player” object to a UI button’s onClick() method once the player is instantiated. It looks like I have everything set up. However when I finally call “button.onClick.AddListener (() => {plyrHmn.plyrFunction();});” I get the following error:

“Assets/Resources/Scripts/instPlyrHmn.cs(12,46): error CS1061: Type UnityEngine.GameObject' does not contain a definition for plyrFunction’ and no extension method plyrFunction' of type UnityEngine.GameObject’ could be found. Are you missing an assembly reference?”

I’m pretty new to Unity and C#. But previously, I have been able to call methods from other objects. It just doesn’t seem to work in this particular case. I am running the “Setup” script on a third-party “mediator” object. Attached are both of the scripts. Thanks

You need to get the component before you can access it’s methods.
plyrHmn is a GameObject, you should be using getComponent() to get the instance of the script from it.

Note that if this button exist in the scene, you can just as easily setup the onclick in the inspector.

A GameObject isn’t a plyrHmn, though it may have the plyrHmn attached, and therefore doesn’t have the plyrFunction available. You need to call GetComponent() on your GameObject to get access to the actual script.

It looks like you found the game object with that name, but not the script.
You probably want:

plyrHmn.GetComponent<plyrHmn>().plyrFunction()

I would suggest that you try to use a public variable or private with the SerializeField attribute, and drag n drop the reference in the inspector, as opposed to using GameObject.Find which is slow and error-prone.
quick edit to echo @Brathnann 's suggestion for adding the OnClick event in the inspector, if possible.

Check out this thread for how to post code directly into the thread, nicely formatted, so you can avoid screenshots of code in the future: Using code tags properly

Thank you guys for the quick replies. I’ll be sure to include the CODE tags in the future. After solving that relatively simple issue. Now when I run the code it tells me line 11 does not reference to an instance of an object. So now the error occurs here Button button = GameObject.Find("UI").GetComponent<Button>();
Sorry for sounding noobish. I’ve made real progress by reading the docs. However I seem to be having issues with this particular script.

Rather, the error occurs when button.onClick.RemoveAllListeners (); is called.

Which means button is null. Do you have a GameObject named UI with a Button script on it?

As mentioned, avoiding GameObject.Find is a good idea, for exact reasons such as this.

It turns out I’m making simple mistakes from staying up all night coding. I corrected the line to Button button = GameObject.Find ("myButton").GetComponent<Button>(); I had my gameObject.name confused with it’s attached component of the same name lol. Goodnight guys. I’m glad to join this communtiy.