Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
-1
Question by Wikened · Dec 21, 2011 at 02:52 PM · guigameobjectnetworkingmultiplayer

Find GameObjects and then add there name to an array?

Hello guys!

Ok. So I have a problem which is absolutely confusing me. Here it is:

  1. My scene is a multiplayer scene.

  2. Its a scene were players operate, edit and spawn trains in a network.

  3. I got the networking working fine :D and when a new peer connects he see's the previously spawned trains and they are in the correct position...

  4. What is not working is the interface where the client loads every train in the scene.

When the player presses "B" the an interface that comes up with 3 options. "Load Trains" "New Train" "Edit Train". When the player presses "Load Trains" A new interface comes up with a list of all trains saved in the array which keeps track of all instantiated trains. (SEE CODE).

BUT when a new player joins or the peer is not creating the train, the train will never show up in his load trains interface. I need a function which updates every 1-2 seconds searching of every game object with the tag "Train" and then get its name and then get its in-game transform. BTW this is all working in single player. Here is the long and confusing Core.js code!

 public 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 = ""; private var toggleReturning : boolean = false;

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

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
 
 searchItemList = GameObject.FindGameObjectsWithTag("Train").name;
 
 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;
 
 Debug.Log("GUI has been updated!");

}

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;
         var ErrorNoName = "";

         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);
         
         toggleReturning = GUI.Toggle(Rect(Screen.width /2 - 100, 175, 100, 25),toggleReturning,"Is Returning?");
         
         GUI.Label(new Rect(Screen.width /2 - 100, 200, 160, 25),ErrorNoName);
     
         if(toggleReturning == false)
         {
             if(GUI.Button(Rect(Screen.width /2 + 50, 150, 60, 25),"Create"))
             {
                 if(newTrainName == "")
                 {
                     // Do nothing... Because no name has been entered
                 }
                 else
                 {
                 
                     var Train : GameObject;
                 
                     Train = Network.Instantiate(TrainType,Vector3(55.96,-16.42,-0.28),Quaternion(0,0,0,0),0);    
                     Trains.Add(Train);
                     Train.name = newTrainName + " " + TrainNextID;
                     searchItemList[TrainNextID] = new SearchObject(newTrainName + " " + TrainNextID);
                     TrainNextID ++;
                     
                 }
         
             }
         }
         else
         {
             if(GUI.Button(Rect(Screen.width /2 + 50, 150, 60, 25),"Create"))
             {
                 if(newTrainName == "")
                 {
                     // Do nothing... Because no name has been entered
                 }
                 else
                 {
                     var TrainReturn : GameObject;
                 
                     TrainReturn = Network.Instantiate(TrainReturnType,Vector3(1259.61,-16.12,13.02),Quaternion(0,180,0,0),0);    
                     Trains.Add(TrainReturn);
                     TrainReturn.name = newTrainName + " " + TrainNextID;
                     searchItemList[TrainNextID] = new SearchObject(newTrainName + " " + TrainNextID);
                     TrainNextID ++;
                 }
             }
         }
     }
 }

}

function OnServerInitialized(){ // Spawnplayer(); }

function OnConnectedToServer(){ // Spawnplayer(); }

function Spawnplayer() { // var Traintype : GameObject = Network.Instantiate(TrainReturnType,1); var Traintype : GameObject; var positionTrain = Vector3;

 Traintype = Network.Instantiate(TrainReturnType,TrainReturnType.position,TrainReturnType.rotation,1);

}

function OnPlayerDisconnected(player: NetworkPlayer) { Debug.Log("Clean up after player " + player); Network.RemoveRPCs(player); Network.DestroyPlayerObjects(player); }

function OnDisconnectedFromServer(info : NetworkDisconnection) { Debug.Log("Clean up a bit after server quit"); Network.RemoveRPCs(Network.player); Network.DestroyPlayerObjects(Network.player);

 /* 
 * Note that we only remove our own objects, but we cannot remove the other players 
 * objects since we don't know what they are; we didn't keep track of them. 
 * In a game you would usually reload the level or load the main menu level anyway ;).
 * 
 * In fact, we could use "Application.LoadLevel(Application.loadedLevel);" here instead to reset the scene.
 */
 Application.LoadLevel(Application.loadedLevel);

}

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

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

}

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

On screen scoring in multiplayer 1 Answer

Searching for gameobjects with a tag and creating a button for each one? 1 Answer

How to make GUI update for all players in a networked game? 1 Answer

How to synchronize GameObjects 2 Answers


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