How to attach script during runtime?

Hello, I am make a simple in unity. In that an object, which I am spawning through Instantiate method requires a reference to 2 scripts. So, how can I refer them to the object script while making them as private.

These are the scripts:-

    private InterfaceManager interfaceManager;
    private PlayerGameplay playerGameplay;

These scripts are on other game objects, right?

I use 2 methods for that, either you search for the gameObject in question with :


Or if there is only one script instance in the scene of your 2 scripts that you want to retrieve you can create a static of the 2 scripts there. For example in the InterfaceManager script you put the code like this:

public static InterfaceManager instance;

    private void Awake()
        instance = this;

And all you have to do is in the script of the instantiated object you get it in the start method like this:

void Start()
        interfaceManager = InterfaceManager.instance;

Hope this help you !