- Home /
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:
My scene is a multiplayer scene.
Its a scene were players operate, edit and spawn trains in a network.
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...
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
}
}