How do you connect values in scripts to gameobjects using code?

Hello. I am a noob trying to recreate a basic version of cookie clicker in unity. I’m currently making the golden cookie effect. I have instantiated a button that is a child of the canvas and shows up. I click on the button expecting it to call the function in the script, but the script on the instance of the button isn’t connected to the MoneyHandler gameobject that the script on the button accesses when pressed. I tried fixing this by searching it up on forums, changing the button script, and connecting it to a prefab but none of these work. Is there a way to connect empty values of scripts like in the attached file to a gameobject using code?
8693442--1173033--image.png

Personally I like this pattern when I’m adding components in code:

Factory Pattern in lieu of AddComponent (for timing and dependency correctness):

Otherwise you must either make that field public so another script can access it, or provide a pubilc method to call to set the value(s).

Use caution with doing stuff super-early, such as Awake() and OnEnable(), both of which execute BEFORE you are given control back after an AddComponent() call.