Loading gameObject during the game

I’m making simple RTS game. In this game, in GUI is button called: “Recruit Unit”. This button will recruit/create new unit/gameObject.
But I don’t know how to do it. I know about Instantiate function, but this function can clone existing objects. But when game will starting there won’t be any objects/units.
So, what should I do it? What is the best option: load object at first on game starting (and keep it somewhere outside the camera view to clone it later), or load it when player request it?

make the object into a prefab. store a reference to that prefab in your script that contains the GUI button. then instantiate the prefab when the button is pressed.

of course if you will be using and reusing recruits throughout your game, you may want to think about creating a pool of recruit objects, so that you wouldn’t have to keep instantiating and destroying objects.

The Instantiate function doesn’t clone existing it just creates a new GameObject after a Prefab. What you have to do:

Create a new Prefab somewhere in your projects by right clicking in the Project Explorer and selecting Create → Prefab

Then you drag your Unit onto it. This way you have your Unit/Gameobject saved in your project. When you now want to recruit a Unity you just have to Instantiate the Prefab.

This way there doesn’t have to be a Unity already in the scene when you start the game.

Hope it works.

Instantiate is really meant to be used on a “blueprint” Asset, which you create ahead of time, called a prefab. It’s one of the basic Unity tricks, and lots of places to read about making/using them.

For building new units, you’d make one prefab of each type. There will probably be a script on them, with variables like moveRate, damage, and so on. Say you have lots of orc types. Instead of making a prefab for each type (could be hundreds if you can change color, trade damage for speed…) you can Instantiate an orc and then make small changes. Can even make it a little larger or pick a random weapon (which you would also Instatiate from one of the weapon prefabs.) The docs and examples here also cover pretty well how to do that.