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
0
Question by TheRichardGamer · Dec 09, 2013 at 08:36 PM · multiplayergameonline

How to search for multiplayer games

Hi, I am trying to make a list of games pop up when I press a button that refreshes the list, and it works fine and so, but there's a problem with the game names. So my problem is that if anyone hosts a game with a different name than the default one, you'd have to enter the name of that game in order for it to pop up, and this is really annoying, how do I fix this so that it shows any game even if the name is different?

Here's my script:

 var SwatPlayer : GameObject;
 var SoldierPlayer : GameObject;
 
 var SwatSpawn : Transform;
 var SoldierSpawn : Transform;
  
 var gameName : String = "Game";
 private var refreshing : boolean = false;
 private var hostData : HostData[];
 
 var ChoosingTeams : boolean = false;
  
  var EmptyName : boolean = false;
  
  var TeamsScript : ChooseTeams;
 
 var Online : boolean = false;
 
 private var RespawnText : GUIText;
  
  
 function OnGUI () {
  
 if(!Network.isClient && !Network.isServer) {
  
  GUI.Box (Rect (Screen.width/2 - 25,Screen.height/2 - 55,150,115), "Multiplayer [Alpha]");
  
  
  gameName = GUI.TextField(Rect(Screen.width/2,Screen.height/2 -10,100,20),gameName);
  
  GUI.Label(Rect(Screen.width/2,Screen.height/2 -30,150,20),"Host a game");
 
 
  
  if(EmptyName){
  
  GUI.Label (Rect (Screen.width/2, Screen.height/2 -12, 430, 20), "Please enter a game name, or your game won't register");
  
  }
  
 
  
  
  if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 10,100,20),"Start Server")) {
  
    TeamsScript.enabled = true;
            Screen.lockCursor = false;
 }
  
  if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 30,100,20),"Refresh List")) {
         Debug.Log("Refresh");
         refreshHostList();
 }
         if(hostData) {
  
         for(var i:int = 0; i<hostData.length; i++) {
        
         if(GUI.Button(Rect(Screen.width/2 - 20,Screen.height/2 + 80,270,20),"Game: " + hostData[i].gameName)) {
                 Network.Connect(hostData[i]);
                 TeamsScript.enabled = true;
                  Screen.lockCursor = false;
        
         }
        
        
 }
  
 }
  
  
 }
  
  
 }
  
  
 function Update () {
  
 
  
  if(String.IsNullOrEmpty(gameName)){
  
  EmptyName = true;
  
  }
  
   else
   
  EmptyName = false;
  
  
  
  
         if(refreshing) {
                 if(MasterServer.PollHostList().Length > 0) {           
                 refreshing = false;
                 Debug.Log(MasterServer.PollHostList().Length);
                 hostData = MasterServer.PollHostList();
                
                
                 }
        
        
         }
  
  
  
 }
  
  
 function startServer () {
  var UseNat = !Network.HavePublicAddress();
         Network.InitializeServer(32,25001, UseNat);
         MasterServer.RegisterHost(gameName, gameName, "This is a beta server");
          Online = true;
           
 }
        
   function OnConnectedToServer () {
   
         if(Network.isClient == true){
       
           TeamsScript.enabled = true;
           
         
       }
  
 }     
        
 function OnServerInitialized () {
  
         Debug.Log("The server is setting up...");
         
      
        
        
 }
  
 
  
 
 
 
  
 function OnMasterServerEvent(mse:MasterServerEvent) {
  
         if(mse == MasterServerEvent.RegistrationSucceeded) {
         Debug.Log("Registered Server with name " + gameName);
        
         }
        
 }
 @RPC       
 function spawnSwat () {
       
         Network.Instantiate(SwatPlayer, SwatSpawn.position, Quaternion.identity, 0);
         Debug.Log("Spawned a player in the team S.W.A.T");
         
        
 }
 
 @RPC
 function spawnSoldier () {
 
         Network.Instantiate(SoldierPlayer, SoldierSpawn.position, Quaternion.identity, 0);
         Debug.Log("Spawned a player in the team Soldiers");
        
 }
 
 
 
        function spawnSwatAndServer () {
         startServer();
         
         if(Online == true){
         Network.Instantiate(SwatPlayer, SwatSpawn.position, Quaternion.identity, 0);
         Debug.Log("Spawned a player in the team S.W.A.T and started the server");
         }
         
        
 }
 
 
 function spawnSoldierAndServer () {
         startServer();
         
         if(Online == true){
         Network.Instantiate(SoldierPlayer, SoldierSpawn.position, Quaternion.identity, 0);
         Debug.Log("Spawned a player in the team Soldiers and started the server");
         }
         
        
 }
 
        
        
        
        
        
        
        
        
 function refreshHostList () {
  
         MasterServer.RequestHostList(gameName);
         refreshing = true;
  
        
        
 }
 
 
 @RPC
 function ReSpawn () {
 
    
     Debug.Log("A player died, the player will respawn once he/she has chosen a team");
     TeamsScript.enabled = true;
 }
 
 
 
 
 
 
 
 
Comment
Add comment · Show 1
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 rednax20 · Dec 09, 2013 at 09:05 PM 0
Share

a little advice, probably best not to paste 220 lines of code onto your question, leave out the parts that don't affect you question, most people won't want to read through all of your code, and will probably stop reading it halfway through because they are just too lazy, while, granted, there are those awesome people who are willing to read through it.

feel free to not really explain your code too much. for instance :

 if (GUI.Button(Rect(10,10,50,50),btnTexture)){
 //code
 }

one can assume that the code is in the OnGUI function even though the code doesn't say so. on first glance people generally don't assume you're stupid and that variables in your code have been defined already.

when you do this just be sure to say that this is not all of your code.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiplayer Script error 2 Answers

how to add option to buy ingame items on my unity game 0 Answers

How to spawn a player in multiplayer on Android with Photon? 1 Answer

Bullet Fire script not working 1 Answer

How to make server files 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