Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dimilicious · Dec 27, 2015 at 10:59 AM · unity 5networkingnetwork

[UNET] OnPlayerConnected is never called

Hi. I have a script with only one method OnPlayerConnected attached to a game object in online scene but the method is never called and there is no Debug.Log output neither on server nor on client. Any ideas?

   void OnPlayerConnected(NetworkPlayer player)
         { 
             Debug.Log("Player " + playerCount + " connected from " + player.ipAddress + ":" + player.port);
         }
 
Comment
Add comment · Show 2
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 apascale · Jan 13, 2016 at 10:29 AM 0
Share

Is the class a NetworkBehaviour?

avatar image Dimilicious apascale · Jan 18, 2016 at 08:43 PM 0
Share

Yes, it is NetworkBehaviour. Does it matter? NetworkBehaviour derives from $$anonymous$$onoBehaviour.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by UNino123 · Jul 30, 2016 at 04:33 PM

As seanr said, its not going to be called on UNET. What I generally do, is have a class that controls all the network messages (simple commands and rpc, connections and disconnections), which, in server is always scanning the NetworkManager.numPlayers; This way, if someone connects or disconnects the number of players will change and you can decide what to do...

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class NetworkMSG : NetworkBehaviour {
     NetworkManager NetMngr;
     NetworkClient meClient;
     public int connectedPlayers=0;
 
     void Start(){
         NetMngr = GameObject.Find ("NetworkManager").GetComponent<NetworkManager> ();
         if (isServer) {
             NetworkServer.RegisterHandler (MsgType.Highest + 2, OnMessageRX); // if you are a server, subscribe to the message handler
             GameObject.FindGameObjectWithTag ("Player").GetComponent<NetPlayerController> ().playerNum = 1;
             GameObject.FindGameObjectWithTag ("Player").GetComponent<NetPlayerController> ().playerRegistered = true;
         
         } 
         if(isClient){
             meClient = NetMngr.client; // if you are client save your network client (to send messages to server through the handler)
         }
     }
 
 
     //CLIENT SCRIPTS
     [ClientRpc]
     void RPCClient(string Garden)
     {
         if (!isClient)
         return;
         
     }
 
 
     //Client send command to server
     public void ClientSendTree(string treeData){
         if (!isClient)
             return;
         TreeMessage Tmessage= new TreeMessage();
         Tmessage.tree = treeData;
         //if (!isServer) {
             meClient.Send (MsgType.Highest + 2, Tmessage);
             Debug.Log ("Just registred this message on network client");
         //} 
     }
 
 
 
     //SERVER SCRIPTS
     public void Update(){
         if (!isServer)
             return;
         
         if (NetMngr.numPlayers != connectedPlayers) { //detect player connection
             if (NetMngr.numPlayers < 2) {
                 connectedPlayers = NetMngr.numPlayers;
             } 
 
             if(NetMngr.numPlayers >connectedPlayers){
                 Debug.Log ("Player connected");
                 StartCoroutine(PlayerConnected (3));//wait five seconds before sending garden
                 connectedPlayers = NetMngr.numPlayers;
             }
             Debug.Log ("NetworkMSG: Players connected to host= "+(connectedPlayers-1).ToString());
             RegisterClients();
         }
     }
 
 
     IEnumerator PlayerConnected(float waitTime){
         yield return new WaitForSeconds(waitTime);
         Debug.Log ("NetworkMSG: Waited for client to connect, to send garden info for: " + waitTime);
         TransmitGarden ();
     }
     public void Reset(){
         if (!isServer)
             return;
 
     }
 
 
     void OnMessageRX(NetworkMessage netmsg){    //receive from user
         Debug.Log ("Message Received");
         if (!isServer)
             return;
         
         TreeMessage msg = netmsg.ReadMessage<TreeMessage> ();//get message
     
         
     }
 }
 
 public class TreeMessage : MessageBase
 {
     public string tree; // in the format zntm (zone, numberOfTree, TreeType, Method)
 }
 
 
Comment
Add comment · Show 2 · 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
avatar image SweatyChair · Oct 31, 2016 at 06:08 AM 0
Share

code is not readable!

avatar image UNino123 · Oct 31, 2016 at 08:28 AM 1
Share

This is the majority of what you would use for a network communication protocol for your basic game, (player connect, client setup, event handler, network messages, RCP calls, etc) ... Hope it is of some use to anyone starting on this. ( I have just started myself, but this code I wrote has worked so far, in various different games I made).

avatar image
0

Answer by seanr · Jan 15, 2016 at 03:42 PM

OnPlayerConnected is from the legacy network API, not UNET.

Comment
Add comment · Show 4 · 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
avatar image Dimilicious · Jan 15, 2016 at 09:53 PM 0
Share

How can i find out if a player connected to the server using UNET?

avatar image apascale Dimilicious · Jan 19, 2016 at 06:08 AM 0
Share

The way I did it was I made a class where the player sends a command to change a Boolean on another class. I then just check if the Boolean has been set to true. I couldnt find any other way but for my needs it works 100% of the time. Hope this helps!

avatar image Dimilicious apascale · Jan 25, 2016 at 11:21 PM 0
Share

Well, i can also count the players using Find GameObjects by Tag "Player" to know if the count has changed. But i want UNET to do it for me.Its really buggy. Photon is light years better than UNET for now.

avatar image sebrk_hiq · Sep 04, 2017 at 02:57 PM 0
Share

How great that it is till there in documentation. Classic Unity.

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

7 People are following this question.

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

Related Questions

Question regarding unity mobile multiplayer game 0 Answers

Unity Network Laggy projectile 0 Answers

How many networked entities Unity can handle with frequent updates? 0 Answers

How can I test my network game if there's no way to run multiple game instance? 2 Answers

Pushed back when applying NetworkTransform 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