• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Wikened · Dec 18, 2011 at 05:44 PM · uiguigameobjectbuttonarray

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

Hi, I have trains w$$anonymous$$ch 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 t$$anonymous$$s?

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)
 {
     // T$$anonymous$$s 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 ++;
     }
 }
 

}

Comment
jahroy

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by jahroy · Dec 18, 2011 at 07:29 PM

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

T$$anonymous$$s 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.

T$$anonymous$$s 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 t$$anonymous$$s example, when the button is clicked, the button is removed from the list.

T$$anonymous$$s also happens to demonstrate how to use a ScrollView.

If you play with t$$anonymous$$s script, you'll see a variable named "Search Item List" in the Inspector. T$$anonymous$$s 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 t$$anonymous$$s 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 C$$anonymous$$cken");
 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 t$$anonymous$$sItem : SearchObject in searchItemList ) {

     /* skip items that have been found */

     if ( t$$anonymous$$sItem.isFound ) {
         continue;
     }

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

     if ( GUI.Button(theRect, t$$anonymous$$sItem.name) ) {
         t$$anonymous$$sItem.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 somet$$anonymous$$ng 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;
 }

}

Comment
Wikened

People who like this

1 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wikened · Dec 18, 2011 at 09:00 PM 0
Share

I got it working! This is amazing... Thank you so much!

avatar image jahroy · Dec 18, 2011 at 09:01 PM 0
Share

You're very welcome.

I'm stoked you can use it.

avatar image Wikened · Dec 18, 2011 at 11:14 PM 0
Share

The script below is what i did :) Changed the transform to a GameObject and then was able to use the method I used before and target each new train. Then I modified the GUI to handle 500 trains and the new feature :)

avatar image jahroy · Dec 18, 2011 at 11:29 PM 0
Share

Glad you got it working.

You should have an array of Trains, though.

You could loop over one thousand trains and print each train's name in the console like this:

 var arrayOfTrains  :  Train [];

 /* populate the array in Awake */

 function Awake ()
 {
     arrayOfTrains = new Train [1000];

     for ( var i = 0; i < 1000; i ++ ) {

         var trainName    :  String = "Train #" + i;
         var freshTrain   :  Train  =  new Train(trainName);

         arrayOfTrains[i] = freshTrain;
     }


 /* test it in Start */

 function Start ()
 {
     for ( var thisTrain : Train in arrayOfTrains ) {
         Debug.Log("Train Name: " + thisTrain.name);
     }
 }

 class Train
 {
     var name     :  String;
     var gameObj  :  GameObject;

     function Train ( n : String )
     {
         name = n;
     }
 }


There's no need to create so many variables and write so much code.

Arrays are very handy.

avatar image

Answer by Wikened · Dec 18, 2011 at 10:44 PM

 // T$$anonymous$$s 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)
 {
     // T$$anonymous$$s 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 t$$anonymous$$sItem : SearchObject in searchItemList) 
         {         
             if (GUI.Button(theRect, t$$anonymous$$sItem.name)) 
             {
                 // Not$$anonymous$$ng 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
 }

}

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Button then instanitates gameobject 1 Answer

(4.6 UI) How to set up a window with x buttons, with a scroll bar? 1 Answer

how to toggle on/off a gameobject with a GUI button ? 3 Answers

Game Object Toggle GUI Button Errors 1 Answer

How do I make a "move pad" on the screen? (Mobile) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges