Is there a way to assing game objects to a script outside of inspector?

I have a town building game and I started making the menu to select the buildings to place. It works like this: when you select a window it sends this information to building placer scripts with info of which game object to place. Currently im working so that all builiding are assinged in inspector, but I wonder if there is another way to do it.

Generally speaking you can write custom editor scripts to manipulate any of your data in Unity the way you manipulate it in the editor.

Best places to start are:

  • tutorials for making editor scripts in general
  • the scripting reference for the API you need to manipulate.

Is there a way to assing game objects to a script outside of inspector?

It’s actually the other way around, you can “assign” a script to a game object:

var theScriptInstance = theObject.AddComponent(typeof(YourScript));

But I don’t think this is the solution you should go for. Instead, you’d have the object already assigned a script that handles the incoming data appropriately. You just tell that script to “create building #123” and it does that, it needn’t start to have a script but it only needs a way to identify what kind of thing it should do, this could be an index into the buildings list or a building prefab reference.