Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Can't receive broadcast message, can anyone help?

Discussion in 'Multiplayer Networking' started by gon777, Mar 15, 2017.

  1. gon777

    gon777

    Joined:
    Dec 20, 2016
    Posts:
    5
    Hi,
    I was able to receive broadcast message yesterday but I have no idea how I broke it :(
    My execution sequence is:

    On my game instance -> call MylobbyManager.instance.Starthost(),
    Then on my editor -> call MylobbyManager.instance.ClientStartListening()

    Can anyone help me look at the code and tell me what is wrong?
    Your help will be much appreciated.

    MyLobbyManager
    Code (CSharp):
    1.      
    2. public class MyLobbyManager : NetworkLobbyManager {
    3.     public static MyLobbyManager instance = null;
    4.     public MyNetworkDiscovery myNetworkDiscovery;
    5.  
    6.     public void Start(){
    7.         if (instance == null) {        
    8.             instance = this;
    9.         } else {      
    10.             Destroy (gameObject);
    11.         }
    12.     }
    13.  
    14.     public override void OnStartHost ()
    15.     {
    16.         base.OnStartHost ();
    17.         myNetworkDiscovery.Initialize ();
    18.         myNetworkDiscovery.StartAsServer ();
    19.     }
    20.  
    21.     public void ClientStartListening(){
    22.         myNetworkDiscovery.Initialize ();    
    23.         myNetworkDiscovery.StartAsClient ();
    24.     }
    25.   }
    26. }
    27.  
    mylobbymanager1.png mylobbymanager2.png

    MyNetworkDiscovery
    Code (CSharp):
    1.  
    2. public class MyNetworkDiscovery : NetworkDiscovery {  
    3.     public override void OnReceivedBroadcast (string fromAddress, string data){
    4.         base.OnReceivedBroadcast (fromAddress, data);
    5.         Debug.Log ("Got Message from (" + fromAddress + ") and (" + data + ")");
    6.     }
    7. }
    8.  
    networkdiscovery.png
     
    Last edited: Mar 15, 2017
  2. gon777

    gon777

    Joined:
    Dec 20, 2016
    Posts:
    5
    Turns out you need to call Initilise() and StartAsServer() AFTER calling StartHost(), not in OnStartHost()
     
  3. gon777

    gon777

    Joined:
    Dec 20, 2016
    Posts:
    5
    Also make sure you linked correct NetworkDiscovery reference in editor