• 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 N17Studio · Aug 07, 2015 at 08:37 PM · networkingclient-servermatchmakingunity multiplayermessages

UNet. Messaging not work. Please help.

Hi, i want implement automatical room searc$$anonymous$$ng in matchmaker. I found or create room. But when i try send message i have error.

Server to clint error:

Empty player list given to NetworkServer.Destroy(), not$$anonymous$$ng to do. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

Client to Server error:

Channels not initialized sending on id '0 UnityEngine.Networking.NetworkConnection:Send(Int16, MessageBase) UNet:OnGUI() (at Assets/UNet.cs:151)

T$$anonymous$$s is my dirty code, more experiments there :)

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 using UnityEngine.Networking.Match;
 using UnityEngine.Networking.Types;
 using UnityEngine.Networking.NetworkSystem;
 
 
 public class UNet : NetworkManager {
 
     NetworkConnection clientConnection;
     NetworkConnection serverConnection;
 
     [SerializeField]
     NetworkLobbyPlayer lobbyPlayer;
 
     void Start () {
         StartMatchMaker();
         matchMaker.ListMatches(0, 20, "", OnMatchList);
         Debug.Log("Start");
     }
 
     void OnMatchList(ListMatchResponse matchList){
         Debug.Log("OnMatchListr");
         foreach(MatchDesc md in matchList.matches){
             Debug.Log(string.Format("roomName = {0}, maxSize = {1}, currentSize = {2}",md.name, md.maxSize, md.currentSize));
             if(md.currentSize > 0 && md.currentSize < md.maxSize){
                 matchMaker.JoinMatch(md.networkId, "", OnMatchJoinedAsClient);
                 return;
             }
         }
         CreateMatch();
     }
 
     public void OnMatchJoinedAsServer(JoinMatchResponse matchJoin){
         //base.OnMatchJoined(matchJoin);
         Debug.Log("OnMatchJoin as Server");
         //StartServer();
         StartHost();
     }
 
     public void OnMatchJoinedAsClient(JoinMatchResponse matchJoin){
         //base.OnMatchJoined(matchJoin);
         Debug.Log("OnMatchJoin as Client");
         StartClient();
     }
 
     void CreateMatch(){
         CreateMatchRequest create = new CreateMatchRequest();
         create.name = "room1";
         create.size = 2;
         create.advertise = true;
         create.password = "";
         NetworkManager.singleton.matchMaker.CreateMatch(create, OnMatchCreate);
     }
 
     public void OnMatchCreate(CreateMatchResponse matchResponse)
     {
         if (matchResponse.success)
             matchMaker.JoinMatch(matchResponse.networkId, "", OnMatchJoinedAsServer);
     }
 
     public override void OnStartServer ()
     {
         Debug.Log ("START SERVER");
         base.OnStartServer ();
         NetworkServer.RegisterHandler(33, OnServer);
         //lobbyPlayer.SendReadyToBeginMessage();
     }
 
     public override void OnStartHost ()
     {
         Debug.Log ("START HOST");
         base.OnStartServer ();
         NetworkServer.RegisterHandler(33, OnServer);
         //lobbyPlayer.SendReadyToBeginMessage();
     }
 
     public override void OnStartClient (NetworkClient client)
     {
         Debug.Log ("START CLIENT");
         base.OnStartClient (client);
         client.RegisterHandler(NetworkMessagesTypes.SERVER_SWITCH, OnServerSwitch);
         client.RegisterHandler(33, OnClient);
         //lobbyPlayer.SendReadyToBeginMessage();
     }
 
     public override void OnServerConnect (NetworkConnection conn)
     {
         Debug.Log ("OnServerConnect");
         //base.OnServerConnect (conn);
         clientConnection = conn;
     }
 
     public override void OnClientConnect (NetworkConnection conn)
     {
         Debug.Log ("OnClientConnect");
         //base.OnServerConnect (conn);
         serverConnection = conn;
         //ClientScene.Ready(conn);
         //ClientScene.AddPlayer(0);
     }
 
     // called when a client is ready
     public virtual void OnServerReady(NetworkConnection conn)
     {
         Debug.Log ("OnServerReady");
         NetworkServer.SetClientReady(conn);
     }
 
     public virtual void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
     {
         Debug.Log ("OnServerAddPlayer");
         NetworkServer.AddPlayerForConnection(conn, new GameObject(), playerControllerId);
     }
     
     void OnServerSwitch(NetworkMessage msg){
         Debug.Log ("SWITCH SERVER");
         StopClient();
         client.handlers.Clear();
         StartServer();
     }
     
     void OnClient(NetworkMessage msg){
         Debug.Log (msg.ReadMessage<NetMSG>().text2);
         msg.conn.Send(33, new NetMSG());
     }
     void OnServer(NetworkMessage msg){
         Debug.Log (msg.ReadMessage<NetMSG>().text);
         msg.conn.Send(33, new NetMSG());
     }
 
     void ServerDisconnect(){
         Debug.Log("ServerDisconnect");
         clientConnection.Send(NetworkMessagesTypes.SERVER_SWITCH, new NetMSG());
         StopServer();
     }
 
     public override void OnStopServer ()
     {
         Debug.Log("OnStopServer");
         base.OnStopServer ();
     }
 
     void OnGUI(){
         if(GUILayout.Button("SwitchServer")){
             ServerDisconnect();
         }
 
         if(GUILayout.Button("Send To Client")){
             clientConnection.Send(33, new NetMSG());
         }
 
         if(GUILayout.Button("Send To Server")){
             serverConnection.Send(33, new NetMSG());
         }
 
         if(GUILayout.Button("Send To ALL")){
             NetworkServer.SendToAll(33, new NetMSG());
         }
     }
 
     void OnApplicationQuit(){
         NetworkManager.singleton.matchMaker.DestroyMatch(matchInfo.networkId, OnMatchDestoy);
         StopHost();
         StopServer();
         Network.Disconnect();
     }
 
     void OnMatchDestoy(BasicResponse response){
         Debug.Log("OnMatchDestoy");
     }
 }
 
 public class NetworkMessagesTypes : MsgType {
     public const short SERVER_SWITCH = 50;
 
 }

Where is ERROR? How create correct automatical matchmaker?

Comment
Serhii-Horun

People who like this

1 Show 3
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 Serhii-Horun · Jan 13, 2016 at 10:54 PM 0
Share

Did you find solution?

avatar image Linearch · Jul 20, 2016 at 03:53 PM 0
Share

Hi, I'm having similar problem here. Did any of you find solution?

avatar image Gameccino · Aug 25, 2016 at 10:31 PM 0
Share

same error here.

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.

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

.io style matchmaking (one button matchmaking) 1 Answer

How to fix "ClientRpc call on un-spawned object" error, using UNET 2 Answers

Unet Matchmaking Europe-North America | cannot see rooms 1 Answer

How to send commands from client objects without authority? 0 Answers

UNET Bug with NetworkHash128 and Saving Prefabs 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