Gathering GameObjects and then creating a button for each GameObject??

Hi,
I have trains which are Instantiated every 12 seconds. So each 12 seconds 2 new trains are spawned. Now, I have a GUI interface and I have a button called “Load Trains”. I want to be able to load each trains name in a new button created for each train. How do I do this?

Here the script so far:

var Trains = new Array();
var TrainType : GameObject;
var TrainReturnType : GameObject;

var GUIUpdate = 2; //How long before the GUI is updated

var TrainNextID = 0;

var Train01Speed = -1;
var Train02Speed = -1;
var Train03Speed = -1;
var Train04Speed = -1;

var Train05Speed = -1;
var Train06Speed = -1;
var Train07Speed = -1;
var Train08Speed = -1;

var EditNetworkUI = false;
var newTrain = false;
var newTrainName : String = “Hello World”;

function Start()
{
var Train : GameObject;
var TrainReturn : GameObject;

Train = Instantiate(TrainType);	
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
TrainNextID ++;

InvokeRepeating("UpdateGUI", 1, GUIUpdate);

yield WaitForSeconds(12);
Train = Instantiate(TrainType);
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
TrainNextID ++;

yield WaitForSeconds(12);
Train = Instantiate(TrainType);
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
TrainNextID ++;

yield WaitForSeconds(12);
Train = Instantiate(TrainType);
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
TrainNextID ++;

}

function Update()
{
if(Input.GetKeyDown(KeyCode.B))
{
if(EditNetworkUI == false)
{
EditNetworkUI = true;
}
else
{
EditNetworkUI = false;
}
}
}

function UpdateGUI()
{
var Train01 = GameObject.Find(“Train 0”); // Get the train
var Train02 = GameObject.Find(“Train 1”); // Get the train
var Train03 = GameObject.Find(“Train 2”); // Get the train
var Train04 = GameObject.Find(“Train 3”); // Get the train

var Train05 = GameObject.Find("Train 4"); // Get the train
var Train06 = GameObject.Find("Train 5"); // Get the train
var Train07 = GameObject.Find("Train 6"); // Get the train
var Train08 = GameObject.Find("Train 7"); // Get the train

Debug.Log("GUI has been updated!");

Train01Speed = Train01.GetComponent("Train").Speed;
Train02Speed = Train02.GetComponent("TrainReturn").Speed;
Train03Speed = Train03.GetComponent("Train").Speed;
Train04Speed = Train04.GetComponent("TrainReturn").Speed;
Train05Speed = Train05.GetComponent("Train").Speed;
Train06Speed = Train06.GetComponent("TrainReturn").Speed;
Train07Speed = Train07.GetComponent("Train").Speed;
Train08Speed = Train08.GetComponent("TrainReturn").Speed;

}

function OnGUI()
{

if(Train01Speed >= 0)
{
	GUI.Box(Rect(15,10,150,25), Train01Speed + "m/s");
}

if(Train02Speed >= 0)
{
	GUI.Box(Rect(15,35,150,25),Train02Speed + "m/s");
}
if(Train03Speed >= 0)
{
	GUI.Box(Rect(15,60,150,25),Train03Speed + "m/s");
}
if(Train04Speed >= 0)
{
	GUI.Box(Rect(15,85,150,25),Train04Speed + "m/s");
}
if(Train05Speed >= 0)
{
	GUI.Box(Rect(15,110,150,25),Train05Speed + "m/s");
}
if(Train06Speed >= 0)
{
	GUI.Box(Rect(15,135,150,25),Train06Speed + "m/s");
}
if(Train07Speed >= 0)
{
	GUI.Box(Rect(15,160,150,25),Train07Speed + "m/s");
}
if(Train08Speed >= 0)
{
	GUI.Box(Rect(15,185,150,25),Train08Speed + "m/s");
}

if(EditNetworkUI == true)
{
	// This is the code for the Interface.
	GUI.Box(new Rect(Screen.width /2 - 250,Screen.height /2 - 300,500,600),"EditNetwork");
	
	if(GUI.Button(Rect(Screen.width /2 - 50, 60, 100, 25), "Load Trains"))
	{
		// Code to load all active trains.
	}
	
	if(GUI.Button(Rect(Screen.width /2 - 50, 85, 100, 25), "New Train"))
	{
		if(newTrain == false)
		{
			newTrain = true;
		}
		else
		{
			newTrain = false;
		}
	}
	
	if(GUI.Button(Rect(Screen.width /2 - 50, 110, 100, 25), "Edit Train"))
	{
		// Code to edit an active train
	}
	
}
else
{
	newTrain = false;
}

if(newTrain == true)
{
	GUI.Label(Rect(Screen.width /2 - 100, 150, 100, 25),"Name:");
	
	newTrainName = GUI.TextField(Rect(Screen.width /2 - 50, 150, 100, 25),newTrainName,10);
	
	if(GUI.Button(Rect(Screen.width /2 + 50, 150, 60, 25),"Create"))
	{
		var Train : GameObject;
		
		Train = Instantiate(TrainType);	
		Trains.Add(Train);
		Train.name = newTrainName + " " + TrainNextID;
		TrainNextID ++;
	}
}

}

Here's an answer from another question that shows how to draw a GUI Button for each item in an array of custom objects.

This was for a person who was writing a game where you were supposed to find a bunch of items. Once you find them, they disappear from a list on the screen.

This is meant to be a simple example. It demonstrates how to create a custom class named SearchObject. It then deomonstrates how to create an array of SearchObjects and finally how to loop over the array in the OnGUI function and draw a button for each one.

In this example, when the button is clicked, the button is removed from the list.

This also happens to demonstrate how to use a ScrollView.

If you play with this script, you'll see a variable named "Search Item List" in the Inspector. This script popuplates that list in the Start function as a test. You could remove the Start function and populate the list with whatever you want by using the Inspector.

Hope this helps. Let me know if you have any questions.

/* an array of SearchObjects */

var searchItemList     :  SearchObject []; 

var scrollRect         :  Rect     =  Rect(200, 200, 220, 220);
var itemRect           :  Rect     =  Rect(0, 0, 200, 600);
var buttonRect         :  Rect     =  Rect(0, 0, 200, 100);
var scrollPos          :  Vector2  =  Vector2.zero;

/*  fill the array with random stuff for testing */

function Start ()
{
    searchItemList = new SearchObject [6];

    searchItemList[0] = new SearchObject("Grenade");    
    searchItemList[1] = new SearchObject("Bunny Slippers");
    searchItemList[2] = new SearchObject("Flame Thrower");
    searchItemList[3] = new SearchObject("Lollypop");
    searchItemList[4] = new SearchObject("Bucket of Chicken");
    searchItemList[5] = new SearchObject("Magnifying Glass");
}

/*  draw a GUI to show what has NOT been found */

function OnGUI ()
{
    scrollPos = GUI.BeginScrollView(scrollRect, scrollPos, itemRect);   

    var theRect : Rect = buttonRect;

    /* loop through each SearchObject in the array */

    for ( var thisItem : SearchObject in searchItemList ) {

        /* skip items that have been found */

        if ( thisItem.isFound ) {
            continue;
        }

        /* draw a button for each SearchObject in the array */

        if ( GUI.Button(theRect, thisItem.name) ) {
            thisItem.isFound = true;
        }

        /* advance the rectangle for the next button */

        theRect.y += theRect.height;
    }

    GUI.EndScrollView();
}

/*  a definition for a simple example class named SearchObject.    */

/*  note that a SearchObject has a transform associated with it.   */
/*  you could assign something to that by dragging an object onto  */
/*  the appropriate slot in the inspector                          */

class SearchObject
{
    var name        :  String;
    var isFound     :  boolean;
    var transform   :  Transform;

    function SearchObject ( theName : String )
    {
        name    =  theName;
        isFound =  false;
    }
}

// This is the array list of Train

var searchItemList : SearchObject ;

var scrollRect : Rect = Rect(660, 155, 166, 450);
var itemRect : Rect = Rect(0, 0, 200, 12500);
var buttonRect : Rect = Rect(1, 0, 150, 25);
var scrollPos : Vector2 = Vector2.zero;

var Trains = new Array();
var TrainType : GameObject;
var TrainReturnType : GameObject;

var GUIUpdate = 2; //How long before the GUI is updated

var TrainNextID = 0;

var Train01Speed = -1;
var Train02Speed = -1;
var Train03Speed = -1;
var Train04Speed = -1;

var Train05Speed = -1;
var Train06Speed = -1;
var Train07Speed = -1;
var Train08Speed = -1;

// GUI Inteface
var EditNetworkUI = false;
var newTrain = false;
var LoadTrain = false;
var newTrainName : String = “”;

function Start()
{
var Train : GameObject;
var TrainReturn : GameObject;
searchItemList = new SearchObject [500];

Train = Instantiate(TrainType);	
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

InvokeRepeating("UpdateGUI", 1, GUIUpdate);

yield WaitForSeconds(12);
Train = Instantiate(TrainType);
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

yield WaitForSeconds(12);
Train = Instantiate(TrainType);
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

yield WaitForSeconds(12);
Train = Instantiate(TrainType);
Trains.Add(Train);
Train.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

TrainReturn = Instantiate(TrainReturnType);	
Trains.Add(TrainReturn);
TrainReturn.name = "Train " + TrainNextID;
searchItemList[TrainNextID] = new SearchObject("Train " + TrainNextID);
TrainNextID ++;

}

function Update()
{
if(Input.GetKeyDown(KeyCode.B))
{
if(EditNetworkUI == false)
{
EditNetworkUI = true;
}
else
{
EditNetworkUI = false;
}
}
}

function UpdateGUI()
{
var Train01 = GameObject.Find(“Train 0”); // Get the train
var Train02 = GameObject.Find(“Train 1”); // Get the train
var Train03 = GameObject.Find(“Train 2”); // Get the train
var Train04 = GameObject.Find(“Train 3”); // Get the train

var Train05 = GameObject.Find("Train 4"); // Get the train
var Train06 = GameObject.Find("Train 5"); // Get the train
var Train07 = GameObject.Find("Train 6"); // Get the train
var Train08 = GameObject.Find("Train 7"); // Get the train

Debug.Log("GUI has been updated!");

Train01Speed = Train01.GetComponent("Train").Speed;
Train02Speed = Train02.GetComponent("TrainReturn").Speed;
Train03Speed = Train03.GetComponent("Train").Speed;
Train04Speed = Train04.GetComponent("TrainReturn").Speed;
Train05Speed = Train05.GetComponent("Train").Speed;
Train06Speed = Train06.GetComponent("TrainReturn").Speed;
Train07Speed = Train07.GetComponent("Train").Speed;
Train08Speed = Train08.GetComponent("TrainReturn").Speed;

}

function OnGUI()
{

if(Train01Speed >= 0)
{
	GUI.Box(Rect(15,10,150,25), Train01Speed + "m/s");
}

if(Train02Speed >= 0)
{
	GUI.Box(Rect(15,35,150,25),Train02Speed + "m/s");
}
if(Train03Speed >= 0)
{
	GUI.Box(Rect(15,60,150,25),Train03Speed + "m/s");
}
if(Train04Speed >= 0)
{
	GUI.Box(Rect(15,85,150,25),Train04Speed + "m/s");
}
if(Train05Speed >= 0)
{
	GUI.Box(Rect(15,110,150,25),Train05Speed + "m/s");
}
if(Train06Speed >= 0)
{
	GUI.Box(Rect(15,135,150,25),Train06Speed + "m/s");
}
if(Train07Speed >= 0)
{
	GUI.Box(Rect(15,160,150,25),Train07Speed + "m/s");
}
if(Train08Speed >= 0)
{
	GUI.Box(Rect(15,185,150,25),Train08Speed + "m/s");
}

if(EditNetworkUI == true)
{
	// This is the code for the Interface.
	GUI.Box(new Rect(Screen.width /2 - 250,Screen.height /2 - 300,500,600),"EditNetwork");
	
	if(GUI.Button(Rect(Screen.width /2 - 50, 60, 100, 25), "Load Trains"))
	{
		if(LoadTrain == false)
		{
			LoadTrain = true;
		}
		else
		{
			LoadTrain = false;
		}
	}
	
	if(GUI.Button(Rect(Screen.width /2 - 50, 85, 100, 25), "New Train"))
	{
		if(newTrain == false)
		{
			newTrain = true;
		}
		else
		{
			newTrain = false;
		}
	}
	
	if(GUI.Button(Rect(Screen.width /2 - 50, 110, 100, 25), "Edit Train"))
	{
		// Code to edit an active train
	}
	
	if(LoadTrain == true)
	{
		scrollPos = GUI.BeginScrollView(scrollRect, scrollPos, itemRect);   
		var theRect : Rect = buttonRect;

		for(var thisItem : SearchObject in searchItemList) 
		{     	
    		if (GUI.Button(theRect, thisItem.name)) 
    		{
				// Nothing yet.
			}
			theRect.y += theRect.height;
		}
		GUI.EndScrollView();
	}
	
}


if(newTrain == true)
{
	GUI.Label(Rect(Screen.width /2 - 100, 150, 100, 25),"Name:");
	
	newTrainName = GUI.TextField(Rect(Screen.width /2 - 50, 150, 100, 25),newTrainName,10);
	
	if(GUI.Button(Rect(Screen.width /2 + 50, 150, 60, 25),"Create"))
	{
		var Train : GameObject;
		
		Train = Instantiate(TrainType);	
		Trains.Add(Train);
		Train.name = newTrainName + " " + TrainNextID;
		searchItemList[TrainNextID] = new SearchObject(newTrainName + " " + TrainNextID);
		TrainNextID ++;
	}
}

}

class SearchObject
{
var name : String;
var transform : GameObject;

function SearchObject ( theName : String )
{
    name = theName;
    transform = GameObject.Find(theName); // Get the train
}

}