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 HuskyPanda213 · Sep 09, 2013 at 03:53 PM · playernetworkkick

How to make a player be disconnected if a match has started?

Im making a game that has networked multiplayer, but now im stuck. The reason is, I want that you can only spawn if you are in the lobby when the host starts the match but if you connect and the match has started that it kicks you(or just remove the server from the browser when the match starts). Though I do not now how to make this function.

Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NetworkMenu : MonoBehaviour {
     
     public string CurrentMenu = "";
     
     public string servername = "Server Name";
     
     public int Port = 24444;
     
     public List<MPPlayer> PlayerList = new List<MPPlayer>();
     
     public GameObject Spawnpoint;
     
     public GameObject Spawnedpl;
     
     public bool matchstarted;
     
     public bool Connected;
     
     public string _Playername;
     
     public static NetworkMenu ins;
     
     public GameObject networkScoreManager;
     
     public ScoreManager score;
     
     public bool _start;
     
     
     public GUIStyle gui_Style;
     
     void Awake(){
         ins = this;
         NextMenu("Main");
         networkScoreManager = GameObject.FindGameObjectWithTag("ScoreManager");
         score = networkScoreManager.GetComponent<ScoreManager>();
         _Playername = PlayerPrefs.GetString("PlayerName");
     }
     
     
     
     void Start(){
         Set_GUI_Style();
     }
     
     private void Set_GUI_Style(){
         gui_Style.alignment = TextAnchor.MiddleCenter;
     }
     
     void OnGUI(){
         if(CurrentMenu == "Main"){
         GUIMain();
         }
         if(CurrentMenu == "Start"){
         GUIStart();
         }
         if(CurrentMenu == "Browser"){
         GUIBrowser();
         }
         Lobby();
     }
     
     
     //GUI
     private void GUIMain(){
     if(!Network.isClient && !Network.isServer){
             
     if(GUI.Button(new Rect(10,10,200,25),"Browser",gui_Style)){
         NextMenu("Browser");
         MasterServer.RequestHostList("354545Battle");
     }
     if(GUI.Button(new Rect(10,40,200,25),"Host",gui_Style)){
         NextMenu("Start");
     }
     _Playername = GUI.TextArea(new Rect(10,70,200,25),_Playername,15,gui_Style);
     if(GUI.Button(new Rect(10,100,200,25),"Save Name",gui_Style)){
     PlayerPrefs.SetString("PlayerName", _Playername);            
     }
     if(GUI.Button(new Rect(10,130,200,25),"Quit",gui_Style)){
         Application.Quit();
     }
     }
     }
     
     private void GUIStart(){
     if(!Network.isClient && !Network.isServer){
     if(GUI.Button(new Rect(10,10,200,25),"Start Server",gui_Style)){
     StartServer();
     }
     if(GUI.Button(new Rect(10,40,200,25),"Back",gui_Style)){
     NextMenu("Main");            
     }
     }
     }
     
     void Update(){
     if(Network.isServer){
     if(Network.connections.Length >= 0 && !matchstarted){
     //Server_SendSettings();
     //Spawn();
     _start = true;
     }
     }
     }
     
     public void Server_SendSettings(){
     networkView.RPC("Player_LoadSettings",RPCMode.All,true);
     networkScoreManager.networkView.RPC("FoundAllPlayers",RPCMode.All,true,Network.connections.Length + 1);
     Debug.Log("Sending Match Settings");    
     }
     
     public void Lobby(){
     if(Network.isServer && !matchstarted){
     if(_start){
     if(GUI.Button(new Rect(10,10,200,25),"Start Match",gui_Style)){
     Server_SendSettings();
     }
     if(GUI.Button(new Rect(10,40,200,25),"End Match",gui_Style)){
     Network.Disconnect();
     Application.LoadLevel(0);
     }
     }        
     }
     }    
     
     private void GUIBrowser(){
     if(!Network.isClient && !Network.isServer){
     if(GUI.Button(new Rect(10,10,200,25),"Back",gui_Style)){
         NextMenu("Main");
     }
     if(GUI.Button(new Rect(10,40,200,25),"Refresh",gui_Style)){
         MasterServer.RequestHostList("354545Battle");
     }
     GUILayout.BeginArea(new Rect(Screen.width / 2, 0,Screen.width / 2,Screen.height),"Server List","box");
     GUILayout.Space(20);
     foreach(HostData hd in MasterServer.PollHostList()){
     GUILayout.BeginHorizontal();
     GUILayout.Label(hd.gameName);
     GUILayout.Label(hd.connectedPlayers + 1 + "/" + hd.playerLimit);
     if(GUILayout.Button("Connect")){
     Network.Connect(hd);
     }
     GUILayout.EndHorizontal();
     }
     GUILayout.EndArea();
     }
     }
     
     void OnPlayerConnected(NetworkPlayer player){
     }
     
     void OnConnectedToServer(){
     Debug.Log("Connecting");
     networkView.RPC("PlayerConnectedToServer",RPCMode.Others,_Playername);
     if(matchstarted){
     Network.Disconnect();
     Application.LoadLevel(0);
     }
     Debug.Log("Done");
     }
     
     public void PlayerLeave(GameObject player){
         Network.Destroy(player);
         networkScoreManager.networkView.RPC("PlayerLeft",RPCMode.All,_Playername);
     }
     
     public void HostQuit(GameObject host){
         networkView.RPC("KickAllPlayers",RPCMode.All);
     }
     
     void OnServerInitialized(){
         
     }
     
     
     public void StartServer(){
         Network.InitializeServer(15,Port,false);
     //    Network.InitializeSecurity();
         MasterServer.RegisterHost("354545Battle",servername,"Hosted Game");
     }
     
     public void Spawn(){
     GameObject PlayerSpawned = Network.Instantiate(Spawnedpl,Spawnpoint.transform.position,Quaternion.identity,1) as GameObject;
     PlayerSpawned.GetComponent<NetworkManager>()._name = _Playername;
     }
     
     //Next Menu Void
     public void NextMenu(string nxt){
         CurrentMenu = nxt;
     }
     
     void OnDisconnectedFromServer(){
         Application.LoadLevel(0);
     }
     
     void OnPlayerDisconnected(NetworkPlayer id){
         Network.DestroyPlayerObjects(id);
         Network.RemoveRPCs(id);
     }
     
     
     
     //RPCs
     
     [RPC]
     void Player_Joined(){
         
     }
     
     [RPC]
     void Player_LoadSettings(bool _matchstarted){
     matchstarted = _matchstarted;
     Debug.Log("Got Settings!");
     Spawn();
     }
     
 }
 
 public class MPPlayer{
     public string PlayerName;
     public NetworkPlayer PlayerNetwork;
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Seizure · Sep 09, 2013 at 04:52 PM

Use a RPC.ALLBuffered function that sets a bool for gamestarted, and if it already has started then remove the button to enter the game.

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

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

How to approach DLC for standalone and web build games? 1 Answer

Problem with identifying players in network 1 Answer

Network Game: How to solve : problem Lag or Refresh frame? 1 Answer

Deleting a player over the network 1 Answer

Spawning Player 2 through network and giving it access to another script 0 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