Filling array with gameObjects

I want to place gameObjects (like buildings etc) selected from an array during runtime. There will be many gameObjects, so I want to avoid manually assigning them in the inspector.

Is there a smooth way to sort/set-up gameObjects so they can be entered into an array automatically?

Any help appreciated, cheers!

You must have some kind of criteria for selecting them. Typically you'd want to collect them by name, or by tag, or by whether they have a specific component (or script) attached. There are various ways to do this:

By Component (or script), for example to collect together every item in the scene which has the "PowerUp" script attached, use FindObjectsOfType. Eg:

var powerUps = FindObjectsOfType(PowerUp);

By Tag, for example to collect all items which have been assigned the "PowerUp" tag, use FindGameObjectsWithTag, like this:

var powerUps = FindGameObjectsWithTag("PowerUp");

And by name, (which is the slowest method - although still sometimes useful), for example to collect all items which include the word "PowerUp" in their name, you have to first find all gameobjects, and then scan through their names. Eg:

var objects = FindObjectsOfType(GameObject);
var powerUps = new Array();
for (var  obj : GameObject in objects) {
    if ("PowerUp")) {

var levelTriggers;

function Start() {
 levelTriggers = FindObjectsOfType(ColTrig);
       if (levelTriggers.length > 0){
       print (levelTriggers);


That should solve your problem. the error

125*UnityException: You are not
allowed to call this function when
declaring a variable.*125

Tells you exactly what is wrong.